Twisted实现web服务器

来源:互联网 发布:apache tomcat安装 编辑:程序博客网 时间:2024/04/28 18:59

新建htm文件夹,在这个文件夹中放入显示的网页文件,在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 = 8888


class ReStructed(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':ReStructed}
resource.indexNames = ['index.html']


reactor.listenTCP(PORT, server.Site(resource))

reactor.run()


python web.py,然后就可以在地址栏使用 127.0.0.1:8888访问默认页面index.html,如果想要访问其他页面,则127.0.0.1:8888/other.html


0 0