`

Learning Twisted(Web)

 
阅读更多
简单的web示例:
#!/usr/bin/python 
'''
File: simple_web.py
Author: Me
Description: 
'''
from twisted.web import server, resource
from twisted.internet import reactor
class Simple(resource.Resource):
    isLeaf = True
    def render_GET(self, request):
        return "<html>Hello,world!</html>"
site = server.Site(Simple())
reactor.listenTCP(8080,site)
reactor.run()

#!/usr/bin/python 
'''
File: simple_web.py
Author: Me
Description: 
'''
from twisted.web import server, resource
from twisted.internet import reactor
class ChildSimple(resource.Resource):
    isLeaf = True
    def render_GET(self, request):
        return "<html>hello,child</html>"
class Simple(resource.Resource):
    def __init__(self):
        """docstring for __init__"""
        resource.Resource.__init__(self)
        self.putChild("", self)
        self.putChild("child",ChildSimple())
    def render_GET(self, request):
        return "<html>Hello,world!</html>"
site = server.Site(Simple())
reactor.listenTCP(8080,site)
reactor.run()

Resource 官方文档说明:
http://twistedmatrix.com/documents/current/api/twisted.web.resource.Resource.html
#!/usr/bin/python 
'''
File: simple_web.py
Author: Me
Description: 
'''
from twisted.web import server, resource
from twisted.internet import reactor
class ChildSimple(resource.Resource):
    isLeaf = True
    def __init__(self,id):
        resource.Resource.__init__(self)
        self.id=id
    def render_GET(self, request):
        return "Hello, No. %s visitor!"% self.id
class Simple(resource.Resource):
    def __init__(self):
        """docstring for __init__"""
        resource.Resource.__init__(self)
        self.putChild("", self)
    def render_GET(self, request):
        return "<html>Hello,world!</html>"
    def getChild(self,path,request):
        return ChildSimple(path)
site = server.Site(Simple())
reactor.listenTCP(8080,site)
reactor.run()





分享到:
评论

相关推荐

    Learning Scrapy azw3 kindle格式 0分

    Paperback: 270 pages Publisher: Packt Publishing - ebooks... Make your crawler super-fast by learning how to tune Scrapy's performance Perform large scale distributed crawls with scrapyd and scrapinghub

    Learning Scrapy 2016无水印pdf 0分

    Paperback: 270 pages Publisher: Packt Publishing - ebooks... Make your crawler super-fast by learning how to tune Scrapy's performance Perform large scale distributed crawls with scrapyd and scrapinghub

Global site tag (gtag.js) - Google Analytics