本文主要是介绍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 Twisted(Web)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!