lost and found ( for me ? )

Python : Simple Web server with Python

Here’s an explanation of how to run simple web server with python script.

# python --version
Python 2.7.3

use os , BaseHTTPServer and SimpleHTTPServer modules
# cat -n Simple_HTTPD.py
    1 #!/usr/bin/env python
    2
    3 import os
    4 import BaseHTTPServer
    5 import SimpleHTTPServer
    6
    7 os.chdir('/home/hattori/www_dir')
    8 server = BaseHTTPServer.HTTPServer(('localhost',80),SimpleHTTPServer.SimpleHTTPRequestHandler)
    9 server.serve_forever()


>>> import BaseHTTPServer
>>> type(BaseHTTPServer)
<type 'module'>
>>> dir(BaseHTTPServer)
['BaseHTTPRequestHandler', 'DEFAULT_ERROR_CONTENT_TYPE', 'DEFAULT_ERROR_MESSAGE', 'HTTPServer', 'SocketServer', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_quote_html', 'catch_warnings', 'filterwarnings', 'mimetools', 'socket', 'sys', 'test', 'time']

>>> import SimpleHTTPServer
>>> type(SimpleHTTPServer)
<type 'module'>
>>> dir(SimpleHTTPServer)
['BaseHTTPServer', 'SimpleHTTPRequestHandler', 'StringIO', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', 'cgi', 'mimetypes', 'os', 'posixpath', 'shutil', 'sys', 'test', 'urllib']
>>>

make a directory for simple web server
# ls /home/hattori/www_dir/
index.html

run python script
mint14-note Python_works # chmod u+x Simple_HTTPD.py
mint14-note Python_works # ./Simple_HTTPD.py

access
# wget -d http://127.0.0.1/index.html -O -
Setting --output-document (outputdocument) to -
DEBUG output created by Wget 1.13.4 on linux-gnu.

URI encoding = `UTF-8'
--2012-12-08 21:32:05--  http://127.0.0.1/index.html
Connecting to 127.0.0.1:80... connected.
Created socket 3.
Releasing 0x0000000001a6de80 (new refcount 0).
Deleting unused 0x0000000001a6de80.

---request begin---
GET /index.html HTTP/1.1
User-Agent: Wget/1.13.4 (linux-gnu)
Accept: */*
Host: 127.0.0.1
Connection: Keep-Alive

---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/2.7.3
Date: Sat, 08 Dec 2012 12:32:05 GMT
Content-type: text/html
Content-Length: 6
Last-Modified: Sat, 08 Dec 2012 12:15:57 GMT

---response end---
200 OK
Registered socket 3 for persistent reuse.
Length: 6 [text/html]
Saving to: `STDOUT'

0% [                                       ] 0           --.-K/s              hello
100%[======================================>] 6           --.-K/s   in 0s      

2012-12-08 21:32:05 (437 KB/s) - written to stdout [6/6]

access log.
# ./Simple_HTTPD.py
127.0.0.1 - - [08/Dec/2012 21:32:05] "GET /index.html HTTP/1.1" 200 -

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.