Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, March 31, 2015

Installing django on fresh ubuntu


I have just installed a ubuntu (latest 64 bit avialable on ubuntu web site) over a freshly installed 'vmware palyer' (again downloaded latest from vmware website)... all comes in FREE as of now.

Installed Ubuntu ...
Now, next steps was to install django server, so I open the terminal and this is how I installed it...

I was following this link for it : https://www.howtoforge.com/tutorial/django-install-ubuntu-14.04/

===============

tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ sqllite
No command 'sqllite' found, did you mean:
 Command 'sqlline' from package 'sqlline' (universe)
 Command 'sqlite' from package 'sqlite' (universe)
sqllite: command not found
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ sudo apt-get install sqlite
[sudo] password for tapesh:
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following extra packages will be installed:
  libsqlite0 sqlite3
Suggested packages:
  sqlite-doc sqlite3-doc
The following NEW packages will be installed:
  libsqlite0 sqlite sqlite3
0 upgraded, 3 newly installed, 0 to remove and 102 not upgraded.
Need to get 182 kB of archives.
After this operation, 754 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/universe libsqlite0 amd64 2.8.17-10ubuntu2 [139 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ trusty/universe sqlite amd64 2.8.17-10ubuntu2 [14.4 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu/ trusty/main sqlite3 amd64 3.8.2-1ubuntu2 [28.9 kB]
Fetched 182 kB in 2s (67.8 kB/s)
Selecting previously unselected package libsqlite0.
(Reading database ... 163905 files and directories currently installed.)
Preparing to unpack .../libsqlite0_2.8.17-10ubuntu2_amd64.deb ...
Unpacking libsqlite0 (2.8.17-10ubuntu2) ...
Selecting previously unselected package sqlite.
Preparing to unpack .../sqlite_2.8.17-10ubuntu2_amd64.deb ...
Unpacking sqlite (2.8.17-10ubuntu2) ...
Selecting previously unselected package sqlite3.
Preparing to unpack .../sqlite3_3.8.2-1ubuntu2_amd64.deb ...
Unpacking sqlite3 (3.8.2-1ubuntu2) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up libsqlite0 (2.8.17-10ubuntu2) ...
Setting up sqlite (2.8.17-10ubuntu2) ...
Setting up sqlite3 (3.8.2-1ubuntu2) ...
Processing triggers for libc-bin (2.19-0ubuntu6.5) ...
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ sudo apt-get install python-setuptools
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following NEW packages will be installed:
  python-setuptools
0 upgraded, 1 newly installed, 0 to remove and 102 not upgraded.
Need to get 230 kB of archives.
After this operation, 830 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/main python-setuptools all 3.3-1ubuntu1 [230 kB]
Fetched 230 kB in 2s (79.3 kB/s)                      
Selecting previously unselected package python-setuptools.
(Reading database ... 163922 files and directories currently installed.)
Preparing to unpack .../python-setuptools_3.3-1ubuntu1_all.deb ...
Unpacking python-setuptools (3.3-1ubuntu1) ...
Setting up python-setuptools (3.3-1ubuntu1) ...
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ sqllite --version
No command 'sqllite' found, did you mean:
 Command 'sqlite' from package 'sqlite' (universe)
 Command 'sqlline' from package 'sqlline' (universe)
sqllite: command not found
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ sqllite -version
No command 'sqllite' found, did you mean:
 Command 'sqlline' from package 'sqlline' (universe)
 Command 'sqlite' from package 'sqlite' (universe)
sqllite: command not found
tapesh@ubuntu:~$ sqlite --version
sqlite: unknown option: --version
Use -help for a list of options.
tapesh@ubuntu:~$ sqlite -version
2.8.17
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ pip --version
The program 'pip' is currently not installed. You can install it by typing:
sudo apt-get install python-pip
tapesh@ubuntu:~$ pip
The program 'pip' is currently not installed. You can install it by typing:
sudo apt-get install python-pip
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ ls
Desktop  Documents  Downloads  examples.desktop  Music  Pictures  Public  Templates  Videos
tapesh@ubuntu:~$ cd Downloads/
tapesh@ubuntu:~/Downloads$ ls
tapesh@ubuntu:~/Downloads$ wget https://bootstrap.pypa.io/get-pip.py
--2015-03-31 06:21:10--  https://bootstrap.pypa.io/get-pip.py
Resolving bootstrap.pypa.io (bootstrap.pypa.io)... 103.245.222.175
Connecting to bootstrap.pypa.io (bootstrap.pypa.io)|103.245.222.175|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1608741 (1.5M) [text/x-python]
Saving to: ‘get-pip.py’

100%[=================================================================================>] 1,608,741    325KB/s   in 5.3s  

2015-03-31 06:21:21 (296 KB/s) - ‘get-pip.py’ saved [1608741/1608741]

tapesh@ubuntu:~/Downloads$
tapesh@ubuntu:~/Downloads$
tapesh@ubuntu:~/Downloads$ ls
get-pip.py
tapesh@ubuntu:~/Downloads$ python get-pip.py
Collecting pip
  Downloading pip-6.0.8-py2.py3-none-any.whl (1.3MB)
    100% |################################| 1.3MB 83kB/s
Installing collected packages: pip

  Exception:
  Traceback (most recent call last):
    File "/tmp/tmpfgWRwc/pip.zip/pip/basecommand.py", line 232, in main
      status = self.run(options, args)
    File "/tmp/tmpfgWRwc/pip.zip/pip/commands/install.py", line 347, in run
      root=options.root_path,
    File "/tmp/tmpfgWRwc/pip.zip/pip/req/req_set.py", line 549, in install
      **kwargs
    File "/tmp/tmpfgWRwc/pip.zip/pip/req/req_install.py", line 754, in install
      self.move_wheel_files(self.source_dir, root=root)
    File "/tmp/tmpfgWRwc/pip.zip/pip/req/req_install.py", line 963, in move_wheel_files
      isolated=self.isolated,
    File "/tmp/tmpfgWRwc/pip.zip/pip/wheel.py", line 234, in move_wheel_files
      clobber(source, lib_dir, True)
    File "/tmp/tmpfgWRwc/pip.zip/pip/wheel.py", line 205, in clobber
      os.makedirs(destdir)
    File "/usr/lib/python2.7/os.py", line 157, in makedirs
      mkdir(name, mode)
  OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/pip-6.0.8.dist-info'

tapesh@ubuntu:~/Downloads$ sudo python get-pip.py
The directory '/home/tapesh/.cache/pip/log' or its parent directory is not owned by the current user and the debug log has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag.
The directory '/home/tapesh/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag.
The directory '/home/tapesh/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag.
Collecting pip
  Downloading pip-6.0.8-py2.py3-none-any.whl (1.3MB)
    100% |################################| 1.3MB 97kB/s
Installing collected packages: pip

Successfully installed pip-6.0.8
tapesh@ubuntu:~/Downloads$
tapesh@ubuntu:~/Downloads$
tapesh@ubuntu:~/Downloads$
tapesh@ubuntu:~/Downloads$
tapesh@ubuntu:~/Downloads$
tapesh@ubuntu:~/Downloads$ cd
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ pip --version
pip 6.0.8 from /usr/local/lib/python2.7/dist-packages (python 2.7)
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ sudo easy_install virtualenv
Searching for virtualenv
Reading https://pypi.python.org/simple/virtualenv/
Download error on https://pypi.python.org/simple/virtualenv/: [Errno -2] Name or service not known -- Some packages may not be found!
Couldn't find index page for 'virtualenv' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
No local packages or download links found for virtualenv
error: Could not find suitable distribution for Requirement.parse('virtualenv')
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ sudo easy_install virtualenv
Searching for virtualenv
Reading https://pypi.python.org/simple/virtualenv/
Best match: virtualenv 12.0.7
Downloading https://pypi.python.org/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz#md5=e08796f79d112f3bfa6653cc10840114
Processing virtualenv-12.0.7.tar.gz
Writing /tmp/easy_install-GXDQBd/virtualenv-12.0.7/setup.cfg
Running virtualenv-12.0.7/setup.py -q bdist_egg --dist-dir /tmp/easy_install-GXDQBd/virtualenv-12.0.7/egg-dist-tmp-TJOjti
warning: no previously-included files matching '*' found under directory 'docs/_templates'
warning: no previously-included files matching '*' found under directory 'docs/_build'
Adding virtualenv 12.0.7 to easy-install.pth file
Installing virtualenv script to /usr/local/bin
Installing virtualenv-2.7 script to /usr/local/bin

Installed /usr/local/lib/python2.7/dist-packages/virtualenv-12.0.7-py2.7.egg
Processing dependencies for virtualenv
Finished processing dependencies for virtualenv
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ virtualenv --no-site-packages django-user
New python executable in django-user/bin/python
Installing setuptools, pip...done.
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ ls
Desktop  django-user  Documents  Downloads  examples.desktop  Music  Pictures  Public  Templates  Videos
tapesh@ubuntu:~$ cd django-user/
tapesh@ubuntu:~/django-user$ ls
bin  include  lib  local
tapesh@ubuntu:~/django-user$ cd..




cd..: command not found
tapesh@ubuntu:~/django-user$
tapesh@ubuntu:~/django-user$
tapesh@ubuntu:~/django-user$
tapesh@ubuntu:~/django-user$
tapesh@ubuntu:~/django-user$ cd ..
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$
tapesh@ubuntu:~$ source django-user/bin/activate
(django-user)tapesh@ubuntu:~$
(django-user)tapesh@ubuntu:~$
(django-user)tapesh@ubuntu:~$
(django-user)tapesh@ubuntu:~$
(django-user)tapesh@ubuntu:~$ ls
Desktop  django-user  Documents  Downloads  examples.desktop  Music  Pictures  Public  Templates  Videos
(django-user)tapesh@ubuntu:~$ cd django-user/
(django-user)tapesh@ubuntu:~/django-user$ ls
bin  include  lib  local
(django-user)tapesh@ubuntu:~/django-user$ \
>
(django-user)tapesh@ubuntu:~/django-user$
(django-user)tapesh@ubuntu:~/django-user$
(django-user)tapesh@ubuntu:~/django-user$
(django-user)tapesh@ubuntu:~/django-user$ easy_install django
Searching for django
Reading https://pypi.python.org/simple/django/
Best match: Django 1.7.7
Downloading https://pypi.python.org/packages/source/D/Django/Django-1.7.7.tar.gz#md5=a62d6598966947d150525ad2ab20fb0c
Processing Django-1.7.7.tar.gz
Writing /tmp/easy_install-L1hzoy/Django-1.7.7/setup.cfg
Running Django-1.7.7/setup.py -q bdist_egg --dist-dir /tmp/easy_install-L1hzoy/Django-1.7.7/egg-dist-tmp-CNW019
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
creating /home/tapesh/django-user/lib/python2.7/site-packages/Django-1.7.7-py2.7.egg
Extracting Django-1.7.7-py2.7.egg to /home/tapesh/django-user/lib/python2.7/site-packages
Adding Django 1.7.7 to easy-install.pth file
Installing django-admin.py script to /home/tapesh/django-user/bin
Installing django-admin script to /home/tapesh/django-user/bin

Installed /home/tapesh/django-user/lib/python2.7/site-packages/Django-1.7.7-py2.7.egg
Processing dependencies for django
Finished processing dependencies for django
(django-user)tapesh@ubuntu:~/django-user$
(django-user)tapesh@ubuntu:~/django-user$
(django-user)tapesh@ubuntu:~/django-user$
(django-user)tapesh@ubuntu:~/django-user$
(django-user)tapesh@ubuntu:~/django-user$
(django-user)tapesh@ubuntu:~/django-user$
(django-user)tapesh@ubuntu:~/django-user$ ls
bin  include  lib  local
(django-user)tapesh@ubuntu:~/django-user$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'1.7.7'
>>>
(django-user)tapesh@ubuntu:~/django-user$


Monday, October 6, 2014

Custom HTTP Server and adding 'custom http header attribute' while POST request

Well, python is used here to create a custom HTTP server which will behave like normal HTTP server for GET request but for POST request, it will send the received POST request header information back in response. Why done?  Just for fun but it can be used to do a lot more.

Python script:

import SimpleHTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler
import urlparse
import logging

class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    
    def do_GET(self):
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
        return

    def do_POST(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write(self.headers)
        return

if __name__ == '__main__':
    from BaseHTTPServer import HTTPServer
    server = HTTPServer(('', 8000), GetHandler)
    print 'Starting server, use <Ctrl-C> to stop'
    server.serve_forever()

Say script name is 'basehttp.py', you can run it using 'python basehttp.py'.

Here is the HTML Page  which play-around with this script.

<html>
<head>
<style>
div {
  border: 5px solid green;
}
</style>
</head>

<body>
<h1>This page will add the below 'Label(s)' into HTTP request header</h1><br>
<table>
<tr><td>Label1</td><td><input type="text" id="api-code1"></input></td></tr>
<tr><td>Label2</td><td><input type="text" id="api-code2"></input></td></tr>
<tr><td>Label3</td><td><input type="text" id="api-code3"></input></td></tr>
<tr><td></td><td><input type="submit" value="Submit" onClick='dojob()'></input></td></tr>
</table>
<br>
<div id="response"></div>
</body>

<script>
function dojob()
{
    xmlhttp=new XMLHttpRequest();

    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        document.getElementById("response").innerHTML = "<h2> Header received on server was:</h2>";
        document.getElementById("response").innerHTML += "<pre>" + xmlhttp.response + "</pre>";
      }
    }

    xmlhttp.open("POST","test.html?t=" + Math.random(), false);
    xmlhttp.setRequestHeader("api-code1", document.getElementById("api-code1").value);
    xmlhttp.setRequestHeader("api-code2", document.getElementById("api-code2").value);
    xmlhttp.setRequestHeader("api-code3", document.getElementById("api-code3").value);
    xmlhttp.send();
}
</script>


Guess what is test.html here ? nothing, we just not using it in our POST request. Math.random() is used to invalidate the browser cache.

Sunday, August 17, 2014

How to run a simple HTTP server using python

python -m SimpleHTTPServer

Just run this and root directly from wherever you are running this will become the core root directly and search for index.htm (or otherwise just do the file listing)