Python基于twisted实现简单的web服务器
本文实例讲述了Python基于twisted实现简单的web服务器,分享给大家供大家参考。具体方法如下:
1. 新建htm文件夹,在这个文件夹中放入显示的网页文件
2. 在htm文件夹的同级目录下,建立web.py,web.py的内容为:
from twisted.web.resource import Resource from twisted.web import server from twisted.web import static from twisted.internet import reactor PORT = 1234 ######################################################################## class ReStructed(Resource): """""" #---------------------------------------------------------------------- def __init__(self, filename, *a): """Constructor""" self.rst = open(filename).read() def render(self, request): return self.rst resource = static.File('htm/') resource.processors = {'.html':ReStructed} resource.indexNames = ['index.html'] reactor.listenTCP(PORT, server.Site(resource)) reactor.run()
3. 安装上twisted 下载地址为:http://twistedmatrix.com/trac/
安装上zope模块:http://old.zope.org/Products/ZopeInterface/3.3.0/zope.interface-3.3.0.tar.gz/swreleasefile_view
5.在命令行中(windows系统)运行:python web.py
6.在浏览器中输入:127.0.0.1:1234,看到效果如下图所示:
希望本文所述对大家的Python程序设计有所帮助
相关推荐
twistedcruller 2020-05-09
Twistedworld 2019-12-22
cairencong 2011-03-07
TwistedNerve 2008-11-24
宿舍 2019-10-20
xuxingzhuang 2012-05-29
Wendywubowen 2019-04-15
efeve 2018-08-08
TwistedFa 2014-11-08
paopao00 2017-05-11
mengdg000 2016-09-25
CoderBoom 2015-09-17
TwistedNerve 2015-09-17
TwistedFate 2015-09-16
twistedcruller 2015-09-15
dxbjfu0 2019-06-28
Twistedjungle 2019-06-21
Fatetwisted 2014-02-24
TwistedFate 2018-08-21