Python
12 Dec 2009
 

tornado, adisp

 
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tornado.httpserver
import tornado.httpclient
import tornado.web
import tornado.options
from adisp import async, process
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
class MainHandler(tornado.web.RequestHandler):
@async
def fetch(self, url, callback):
http = tornado.httpclient.AsyncHTTPClient()
http.fetch(url, self.async_callback(callback))
@tornado.web.asynchronous
@process
def get(self, url):
response = yield self.fetch(url)
if response.error:
raise tornado.web.HTTPError(500, 'Ooops')
self.write(response.body)
self.finish()
application = tornado.web.Application([
(r"/(.*)", MainHandler),
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()