retry proxy on tornado server

 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
38
39
40
41
42
43
import tornado.httpserver
import tornado.httpclient
import tornado.ioloop
import tornado.options
import tornado.web
import logging
import time
from functools import partial
class MainHandler(tornado.web.RequestHandler):
def _prep_req(self, url):
return tornado.httpclient.HTTPRequest(url=url,
connect_timeout=10.0, request_timeout=10.0)
@tornado.web.asynchronous
def get(self, url, retry = 0):
http = tornado.httpclient.AsyncHTTPClient()
request = self.httpclient.HTTPRequest(url=url,
connect_timeout=10.0, request_timeout=10.0)
http.fetch(request, callback=self.async_callback(
partial(self.on_response, retry=2, url = url) ) )
def on_response(self, response, retry= 0, url= None):
if response.error:
if retry == 0:
raise tornado.web.HTTPError(500)
elif url is not None:
self.get(url, retry - 1)
else:
self.write(response.body)
self.finish()
def main():
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/(.*)", MainHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()