Debian Codegraph @

examples/fromtwisted.py

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
•1 #!/usr/bin/python3
2 """
3 An example of using Crochet from a normal Twisted application.
4 """
5
6 import sys
7
8 from crochet import no_setup, wait_for
9 # Tell Crochet not to run the reactor:
10 no_setup()
11
12 from twisted.internet import reactor
13 from twisted.python import log
14 from twisted.web.wsgi import WSGIResource
15 from twisted.web.server import Site
16 from twisted.names import client
17
18 # A WSGI application, will be run in thread pool:
19 def application(environ, start_response):
20 start_response('200 OK', [])
21 try:
22 ip = gethostbyname('twistedmatrix.com')
23 return "%s has IP %s" % ('twistedmatrix.com', ip)
•24 except Exception as e:
25 return 'Error doing lookup: %s' % (e,)
26
27 # A blocking API that will be called from the WSGI application, but actually
28 # uses DNS:
29 @wait_for(timeout=10)
30 def gethostbyname(name):
31 d = client.lookupAddress(name)
32 d.addCallback(lambda result: result[0][0].payload.dottedQuad())
33 return d
34
35 # Normal Twisted code, serving the WSGI application and running the reactor:
36 def main():
37 log.startLogging(sys.stdout)
38 pool = reactor.getThreadPool()
39 reactor.listenTCP(5000, Site(WSGIResource(reactor, pool, application)))
40 reactor.run()
41
42 if __name__ == '__main__':
43 main()