使用Twisted实现一个简单Web服务器
来源:互联网 发布:婚纱影楼网络推广 编辑:程序博客网 时间:2024/06/03 02:44
我的原文发在Python版,这里也发一个,好增加点Python版的人气。希望支持。下面是原文:
运行环境:Python 2.3+Twisted的py-23安装版本
自己实现Web服务器的优点就不用说太多了,主要是能控制具体的实现。也能按照自己的习惯实现互动方式。
而Twisted在tcp以下是C写的,ip和udp部分应该是C和Python的混合产物,而http smtp等则是Python的,自己能很好的扩充。
下面来看个具体的例子:
首先你需要编辑一个html为结尾的文件名放到你的htm目录下。
然后在htm的上一级目录建立一个文件,文件名为web.py,内容如下:
代码:
代码:
PORT = 80#这个是80,如果你的端口被占用了,换成其他的
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
resource = static.File('./htm/')
resource.processors = { '.html' : ReStructured }
resource.indexNames = [ 'index.html']
reactor.listenTCP(
PORT,
server.Site( resource )
)
reactor.run( )
在控制台下进入目录输入 python web.py,然后打开浏览器,输入http://127.0.0.1,看到你的站点了吗?
引自:http://blog.csdn.net/meijingsong/article/details/474678
运行环境:Python 2.3+Twisted的py-23安装版本
自己实现Web服务器的优点就不用说太多了,主要是能控制具体的实现。也能按照自己的习惯实现互动方式。
而Twisted在tcp以下是C写的,ip和udp部分应该是C和Python的混合产物,而http smtp等则是Python的,自己能很好的扩充。
下面来看个具体的例子:
首先你需要编辑一个html为结尾的文件名放到你的htm目录下。
然后在htm的上一级目录建立一个文件,文件名为web.py,内容如下:
代码:
代码:
PORT = 80#这个是80,如果你的端口被占用了,换成其他的
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
resource = static.File('./htm/')
resource.processors = { '.html' : ReStructured }
resource.indexNames = [ 'index.html']
reactor.listenTCP(
PORT,
server.Site( resource )
)
reactor.run( )
在控制台下进入目录输入 python web.py,然后打开浏览器,输入http://127.0.0.1,看到你的站点了吗?
引自:http://blog.csdn.net/meijingsong/article/details/474678
- 使用Twisted实现一个简单Web服务器
- twisted 实现简单的web服务器
- twisted实现web服务器
- Twisted实现web服务器
- 使用Twisted建立web服务器
- 使用twisted的简单服务器
- 使用twisted实现Python服务器
- twisted04 使用twisted.web搭建简单文件服务器
- Twisted Web服务器开发
- WinCE下实现一个简单WEB服务器
- 一个简单web服务器的实现
- 一个简单web服务器的java实现
- Java 实现一个简单的web服务器
- java实现一个简单的Web服务器
- java实现一个简单的Web服务器
- 实现一个简单的web服务器
- 实现一个简单的Web服务器
- Java实现一个简单的Web服务器
- 8--8将数组a中n个整数按相反顺序存放
- Jboss数据库加密
- WCF入门篇--实现简单的HelloWorld效果
- hdu 1997 汉诺塔 判断某一状态是否是在最优路径中
- 迅雷方舟与花瓣:不一样的“瀑布流”
- 使用Twisted实现一个简单Web服务器
- how to trouble shoot thread hangs
- js操作DropDownList
- 框架frame\frameset\iframe的区别
- OracleParameter 详解
- Android 键盘相关一
- 使用python来嗅探局域网内的QQ号码
- 十四周任务2
- 使用Python访问Windows的注册表