辛夷坞

涧户寂无人,纷纷开且落

By - Christen

Python Simple HTTP Server

同事询问Mac下的类HFS软件,想起Python的好:

$ cd /home/christen
$ python -m SimpleHTTPServer 8000


我们的HTTP服务在8000号端口上侦听。你会得到下面的信息:

Serving HTTP on 0.0.0.0 port 8000 ...

服务URL:http://192.168.1.1:8000
如果你只想让这个HTTP服务器服务于本地环境,那么,你需要定制一下你的Python的程序,下面是一个示例:

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()

Leave a Reply

Your email address will not be published.
*
*