天道酬勤,唯爱学可成才

超简单搭建支持python的web服务器

超简单搭建支持python的web服务器

web1.py

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

web2.py

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

  ds

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

Python代码
  1. from CGIHTTPServer import CGIHTTPRequestHandler   
  2. from BaseHTTPServer import HTTPServer       
  3. server_address=('',7777)   
  4. httpd = HTTPServer(server_address, CGIHTTPRequestHandler)   
  5. httpd.serve_forever()  
以下这些是需要安装了 twisted 才能使用的
web4.py

Python代码
  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. class ReStructured( Resource ):
  6.     def __init__( self, filename, *a ):                                           
  7.         self.rst = open( filename ).read( )
  8.     def render( self, request ):  
  9.         return self.rst
  10.     PORT=8888
  11.     resource = static.File('/')                                                     
  12.     resource.processors = { '.html'  : ReStructured }                                 
  13.     resource.indexNames = [ 'index.html']                                     
  14.     reactor.listenTCP(PORT,server.Site( resource ))
  15.     reactor.run( )  
web5.py, 这是又是支持cgi的,又是需要twisted模块的,也是需要在cgi-bin目录下执行

Python代码
  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的模块集成得太多功能了.
python超简单的web服务器。

 

 

 

【冲冠】1200万像素数码相机 超薄超迷你 当摄像头 有声摄像 全能
200 

Tags: python, web, 服务器

« 上一篇 | 下一篇 »

只显示10条记录相关文章

Web 设计与开发终极资源大全(下) (浏览: 643, 评论: 0)
Web 设计与开发终极资源大全(上) (浏览: 639, 评论: 0)
mysql服务器的一些优化记录 (浏览: 1187, 评论: 0)
Linux下memcached缓存服务器的应用 (浏览: 1226, 评论: 0)
ADODB 与 ADODB_lite (浏览: 1496, 评论: 0)

Trackbacks

点击获得Trackback地址,Encode: UTF-8 点击获得Trackback地址,Encode: GB2312 or GBK 点击获得Trackback地址,Encode: BIG5

发表评论

评论内容 (必填):