【转】python超简单的web服务器
2013-05-09 11:35
330 查看
web1.py
web2.py
web3.py , cgi server ,7777端口, 在web3.py执行目录下新建cgi-bin目录 , 在cgi-bin目录写hello.py
web3.py
hello.py
以下这些是需要安装了 twisted 才能使用的
web4.py
web5.py, 这是又是支持cgi的,又是需要twisted模块的,也是需要在cgi-bin目录下执行,上边的hello.py也能用
当然,想实现复杂功能还是需要自己搞代码的,只不过想惊叹python的模块集成得太多功能了.
python超简单的web服务器。
启动web.py
http://localhost:8080/cgi-bin/hello.py
1 2 3 | #!/usr/ web1.py
web3.py
web4.py
python超简单的web服务器。 bin/python import SimpleHTTPServer SimpleHTTPServer . test ( ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #!/usr/bin/python import SimpleHTTPServer import SocketServer import os PORT = 80 WEBDIR = "f:/python语言学习" class Handler ( SimpleHTTPServer . SimpleHTTPRequestHandler ) : def translate_path ( self , path ) : os . chdir ( WEBDIR ) return SimpleHTTPServer . SimpleHTTPRequestHandler . translate_path ( self , path ) try : httpd = SocketServer . TCPServer ( ( "" , PORT ) , Handler ) "dir %s serving at port %s" % ( repr ( WEBDIR ) , PORT ) httpd . serve_forever ( ) except : pass |
web3.py
1 2 3 4 5 | from CGIHTTPServer import CGIHTTPRequestHandler from BaseHTTPServer import HTTPServer server_address = ( '' , 7777 ) httpd = HTTPServer ( server_address , CGIHTTPRequestHandler ) httpd . serve_forever ( ) |
1 2 3 4 5 6 7 8 | #!c:/Python24/python.exe "HTTP/1.0 200 OK" "Content-Type: text/html" "" "<p>" "Hello World!" "</p>" |
web4.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | from twisted . web . resource import Resource from twisted . web import server from twisted . web import static from twisted . internet import reactor class ReStructured ( Resource ) : def __init__ ( self , filename , * a ) : self . rst = open ( filename ) . read ( ) def render ( self , request ) : return self . rst PORT = 8888 resource = static . File ( '/' ) resource . processors = { '.html' : ReStructured } resource . indexNames = [ 'index.html' ] reactor . listenTCP ( PORT , server . Site ( resource ) ) reactor . run ( ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # -*- coding: utf-8 -*- from twisted . internet import reactor from twisted . web import static , server , twcgi from twisted . web . resource import Resource class Collection ( Resource ) : def render_GET ( self , request ) : return "hello world 你好" root = static . File ( './' ) root . putChild ( '' , Collection ( ) ) root . putChild ( 'img' , static . File ( './img' ) ) root . putChild ( 'cgi-bin' , twcgi . CGIDirectory ( 'cgi-bin' ) ) reactor . listenTCP ( 80 , server . Site ( root ) ) reactor . run ( ) |
python超简单的web服务器。
启动web.py
http://localhost:8080/cgi-bin/hello.py
相关文章推荐
- 用Python建立最简单的web服务器
- Python在web开发中简单服务器的搭建与运行问题
- 用Python建立最简单的web服务器
- Python 实现简单 Web 服务器
- 用Python建立最简单的web服务器
- 利用python自带的包可以建立简单的web服务器
- 最简单的python web服务器
- python 建立简单的web服务器
- 用Python建立最简单的web服务器
- Python基于twisted实现简单的web服务器
- Python实现简单的Web服务器
- 使用Python或Node创建简单web服务器和FTP服务器实现文件共享
- python快速建立超简单的web服务器的实现方法
- 利用python2.7自带的简单的web服务器SimpleHTTPServer实现web页面的访问
- Python在web开发中简单服务器的搭建与运行问题
- python 启动简单web服务器
- 用Python自带的包建立简单的web服务器
- Python自带包建立简单web服务器
- 用一行python代码做一个简单的web服务器
- python 30行写超简单的web服务器