python超简单的web服务器

web1.py

  1. #!/usr/bin/python
  2. import SimpleHTTPServer
  3. SimpleHTTPServer.test()


web2.py

  1. #!/usr/bin/python
  2. import SimpleHTTPServer
  3. import SocketServer
  4. import os
  5.  
  6. PORT = 80
  7. WEBDIR = "f:/python语言学习"
  8.  
  9. class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
  10.     def translate_path(self, path):
  11.         os.chdir(WEBDIR)
  12.         return SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(self,path)
  13.  
  14. try:
  15.     httpd = SocketServer.TCPServer(("", PORT), Handler)
  16.     print "dir %s serving at port %s"%(repr(WEBDIR), PORT)
  17.     httpd.serve_forever()
  18. except:pass

web3.py , cgi server ,7777端口, 在web3.py执行目录下新建cgi-bin目录 , 在cgi-bin目录写hello.py

web3.py

  1. from CGIHTTPServer import CGIHTTPRequestHandler 
  2. from BaseHTTPServer import HTTPServer     
  3. server_address=('',7777) 
  4. httpd = HTTPServer(server_address, CGIHTTPRequestHandler) 
  5. httpd.serve_forever()

hello.py

  1. #!c:/Python24/python.exe
  2.  
  3. print "HTTP/1.0 200 OK"
  4. print "Content-Type: text/html"
  5. print ""
  6. print "<p>"
  7. print "Hello World!"
  8. print "</p>"

以下这些是需要安装了 twisted 才能使用的
web4.py

  1. from twisted.web.resource import Resource                                       
  2. from twisted.web import server                                                   
  3. from twisted.web import static                                                   
  4. from twisted.internet import reactor
  5.                                                                                                               
  6.                                                       
  7. class ReStructured( Resource ):                                                                                                                                 
  8.    def __init__( self, filename, *a ):                                         
  9.        self.rst = open( filename ).read( )                                                                                                              
  10.                                                                                 
  11.    def render( self, request ):
  12.        return self.rst               
  13. PORT=8888                                                                               
  14.  
  15. resource = static.File('/')                                                   
  16. resource.processors = { '.html'  : ReStructured }                               
  17. resource.indexNames = [ 'index.html']                                   
  18.                                                                                 
  19. reactor.listenTCP(                                                               
  20.        PORT,                                                                   
  21.        server.Site( resource )                                                 
  22.        )                                                                       
  23. reactor.run( )

web5.py, 这是又是支持cgi的,又是需要twisted模块的,也是需要在cgi-bin目录下执行,上边的hello.py也能用

  1. # -*- coding: utf-8 -*-
  2. from twisted.internet import reactor
  3. from twisted.web import static, server, twcgi
  4. from twisted.web.resource import Resource
  5.  
  6. class Collection(Resource):
  7.         def render_GET(self, request):
  8.                 return "hello world 你好"
  9.  
  10. root = static.File('./')
  11. root.putChild('', Collection())
  12. root.putChild('img', static.File('./img'))
  13. root.putChild('cgi-bin', twcgi.CGIDirectory('cgi-bin'))
  14. reactor.listenTCP(80, server.Site(root))
  15. reactor.run()

当然,想实现复杂功能还是需要自己搞代码的,只不过想惊叹python的模块集成得太多功能了.

posted @ 2011-07-29 16:08  乱炖er  阅读(594)  评论(0编辑  收藏  举报