If you need a simple Web Server, but you don't want to download and install the complex HTTP service programs, such as: Apache,iss. Well, Python might help you. Use Python to complete a simple built-in HTTP server. As a result, you can display your directories and files in HTTP. Tiao only one thing to do, is to install a python.
In fact, this is a very useful way to share files. It is simple to implement a tiny HTTP service program, which requires only one command line under Python. Here's the command line: (assuming we need to share our directory /home/haoel and the IP address is 192.168.1.1)
12 |
$ cd /home/haoel $ python -m SimpleHTTPServer |
That's fine, and our HTTP service listens on port No. 8000. You will get the following information:
Serving HTTP on 0.0.0.0 Port 8000 ...
You can open your browser (ie or Firefox) and enter the following URL:
http://192.168.1.1:8000
If your directory has a file name named index.html, then this file will become a default page, if there is no such file, then the directory list will be displayed.
If you want to change the port number, you can use the following command:
1 |
$ python -m SimpleHTTPServer 8080 |
If you just want this HTTP server to serve your local environment, then you need to customize your Python program, here's an example:
12345678910111213141516171819 |
import sys
import BaseHTTPServer
from SimpleHTTPServer
import SimpleHTTPRequestHandler
HandlerClass
= SimpleHTTPRequestHandler
ServerClass
= BaseHTTPServer.HTTPServer
Protocol
= "HTTP/1.0"
if sys.argv[
1
:]:
port
= int
(sys.argv[
1
])
else
:
port
= 8000
server_address
= (
‘127.0.0.1‘
, port) HandlerClass.protocol_version
= Protocol
httpd
= ServerClass(server_address, HandlerClass)
sa
= httpd.socket.getsockname()
print "Serving HTTP on"
, sa[
0
],
"port"
, sa[
1
],
"..."
httpd.serve_forever()
|
Note: All of these things can work under Windows or Cygwin.
Original address: http://coolshell.cn/articles/1480.html
A very simple Python HTTP service