associação pythonbrasil[11] django zope/plone planet Início Logado como (Entrar)

TwistedWeb

Este é um simples exemplo do twisted.web que pode ser usado tanto para depurar requisicoes de clients web quanto para ambientar-se com a arquitetura poderosa e flexivel do twisted.web.

   1 import os.path as path
   2 import pprint
   3 
   4 import twisted.internet.reactor as reactor
   5 import twisted.web.resource as resource
   6 import twisted.web.server as server
   7 
   8 class Info(resource.Resource):
   9     isLeaf = True
  10     def render(self, request):
  11         request.setHeader('Content-Type', 'text/plain; charset=utf-8')
  12         request.setHeader('Cache-Control', 'no-cache')
  13         request.setHeader('Pragma', 'no-cache')
  14         request.setHeader('Expires', '0')
  15 
  16         res = []
  17 
  18         res.append(('request', request))
  19         res.append(('request.site', request.site))
  20         res.append(('request.site.protocol', request.site.protocol))
  21         res.append(('request.getSession()', request.getSession()))
  22         res.append(('len(request.getSession().__dict__)', len(request.getSession().__dict__)))
  23         res.append(('request type', type(request)))
  24         res.append(('request class', request.__class__))
  25         res.append(('request.method', request.method))
  26         res.append(('request.uri', request.uri))
  27         res.append(('request.path', request.path))
  28         res.append(('request.prepath', request.prepath))
  29         res.append(('request.postpath', request.postpath))
  30         res.append(('request.getHost()', list(request.getHost())))
  31         res.append(('request.prePathURL()', request.prePathURL()))
  32         res.append(('request.args', request.args))
  33         res.append(('request.transport', request.transport))
  34         res.append(('request.transport.hostname', request.transport.hostname))
  35         res.append(('request.transport.protocol', request.transport.protocol))
  36         res.append(('request.transport.reactor', request.transport.reactor))
  37         res.append(('request.transport.getPeer()', list(request.transport.getPeer())))
  38         res.append(('request.transport.getHost()', list(request.transport.getHost())))
  39         res.append(('request.transport.getHandle()', request.transport.getHandle()))
  40         res.append(('request.transport.server', request.transport.server))
  41         res.append(('request.transport.server.socket', request.transport.server.socket))
  42         res.append(('request.transport.server.socketType', request.transport.server.socketType))
  43         res.append(('request.transport.server.port', request.transport.server.port))
  44         res.append(('request.getAllHeaders()', request.getAllHeaders()))
  45         res.append(('request.content.getvalue()', request.content.getvalue()))
  46 
  47         return '\n'.join(['---> ' + i[0] + ': ' + pprint.pformat(i[1]) for i in res])
  48 
  49 class RootResource(resource.Resource):
  50         def render(self, request):
  51                 request.redirect('/info')
  52                 return ''
  53         def getChild(self, path, request):
  54                 if not path:
  55                         return self
  56                 return resource.Resource.getChild(self, path, request)
  57 
  58 # unleash
  59 
  60 site = server.Site(RootResource())
  61 site.resource.putChild('info', Info())
  62 
  63 port = 8090
  64 
  65 if __name__ == '__main__':
  66     reactor.listenTCP(port, site)
  67     reactor.run()