用Python搭建简单的HTTP Server

Python中自带Web服务器模块,通过它可以建立简单的web服务器。

命令

python -m WebServerModule [Port]
注意
  • 默认的端口号为8000
  • 服务器根目录就是运行python的工作目录。

使用方法

直接在命令行中启动。
908869@CNSHA05NB70810 ~
$ python /usr/lib/python2.6/SimpleHTTPServer.py
Serving HTTP on 0.0.0.0 port 8000 ...
在python运行环境中启动。
908869@CNSHA05NB70810 ~
$ python
Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01)
[GCC 4.3.4 20090804 (release) 1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import SimpleHTTPServer
>>> SimpleHTTPServer.test()
Serving HTTP on 0.0.0.0 port 8000 ...
开启后,然后就可以在浏览器中输入
http://localhost:port/directory
访问服务器资源。 
 
如http://localhost:8000/index.htm(当然index.htm文件得自己创建)
 
其他机器也可以通过服务器的IP地址来访问。
 
这里的“Web服务器模块”有如下三种:
  • BaseHTTPServer:提供基本的Web服务和处理器类,分别是HTTPServer和BaseHTTPRequestHandler。
  • SimpleHTTPServer:包含执行GET和HEAD请求的SimpleHTTPRequestHandler类。
  • CGIHTTPServer:包含处理POST请求和执行CGIHTTPRequestHandler类。




posted @ 2012-01-18 15:32  __BSD__  阅读(2772)  评论(0编辑  收藏  举报