Anton Shestakov <engored@ya.ru>, Sat, 29 Nov 2014 13:25:15 +0800
find-closest: readme
async-http-demo/client.py
Permissions: -rwxr-xr-x
import tornado.httpserver import tornado.httpclient from tornado.options import define, options define('port', default=8888, help='run on the given port', type=int) define('debug', metavar='[True]|False', default=False, type=bool, help='enable Tornado debug mode (e.g. restart on source changes)') class WeatherClient(object): self.http_client = tornado.httpclient.AsyncHTTPClient() self.span_re = re.compile(r'(?<=<div class="b-thermometer__now">).*?(?=</div>)', re.S | re.M) body = self.span_re.findall(request.body.decode('utf-8'))[0] self.http_client.fetch('http://pogoda.yandex.ru/moscow/', callback=prepare) class WeatherHandler(tornado.web.RequestHandler): @tornado.web.asynchronous weather_client = WeatherClient() weather_client.get(callback=self.callback) def callback(self, body): self.finish('<center>%s</center>' % body) class AsyncClient(tornado.web.Application): tornado.web.Application.__init__(self, handlers, debug=options.debug) tornado.options.parse_command_line() http_server = tornado.httpserver.HTTPServer(AsyncClient()) http_server.listen(options.port) logging.info('Starting on 127.0.0.1:{0}'.format(options.port)) tornado.ioloop.IOLoop.instance().start() if __name__ == '__main__':