用python建立最简单的web服务器

利用python自带的包可以建立简单的web服务器。在dos里cd到准备做服务器根目录的路径下,输入命令:

python -m web服务器模块 [端口号,默认8000] 例如:

python -m simplehttpserver 8080

然后就可以在浏览器中输入

http://localhost:端口号/路径 来访问服务器资源。 例如:

http://localhost:8080/index.htm(当然index.htm文件得自己创建) 其他机器也可以通过服务器的ip地址来访问。

这里的“web服务器模块”有如下三种:

basehttpserver: 提供基本的web服务和处理器类,分别是httpserver和basehttprequesthandler。
simplehttpserver: 包含执行get和head请求的simplehttprequesthandler类。
cgihttpserver: 包含处理post请求和执行cgihttprequesthandler类。

发表评论