--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore Sat Jan 12 15:36:36 2013 +0900
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/async-http-demo/client.py Sat Jan 12 15:36:36 2013 +0900
+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)
+ def get(self, callback):
+ 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):
+ (r'/', WeatherHandler),
+ 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__':
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/http-methods-demo/client.py Sat Jan 12 15:36:36 2013 +0900
+import tornado.httpserver
+import tornado.httpclient
+from tornado.options import define, options
+define('port', default=9999, help='run on the given port', type=int)
+define('server_port', default=8888, help='assume server is running on given port', type=int)
+def handler_factory(method):
+ class ResultingHandler(tornado.web.RequestHandler):
+ @tornado.web.asynchronous
+ http_client = tornado.httpclient.AsyncHTTPClient()
+ if method in ('POST', 'PUT'):
+ body = 'I make simple_httpclient happy :)'
+ 'http://localhost:%i/' % options.server_port,
+ callback=self.callback,
+ def callback(self, response):
+ self.finish(response.body)
+ return ResultingHandler
+class MethodClient(tornado.web.Application):
+ (r'/(?:get)?/?', handler_factory('GET')),
+ (r'/head/?', handler_factory('HEAD')),
+ (r'/post/?', handler_factory('POST')),
+ (r'/put/?', handler_factory('PUT')),
+ (r'/delete/?', handler_factory('DELETE')),
+ tornado.web.Application.__init__(self, handlers)
+ logging.info('Client started')
+ tornado.options.parse_command_line()
+ http_server = tornado.httpserver.HTTPServer(MethodClient())
+ http_server.listen(options.port)
+ tornado.ioloop.IOLoop.instance().start()
+if __name__ == '__main__':
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/http-methods-demo/server.py Sat Jan 12 15:36:36 2013 +0900
+import tornado.httpserver
+from tornado.options import define, options
+define('port', default=8888, help='run on the given port', type=int)
+class MethodHandler(tornado.web.RequestHandler):
+ self.finish({'success': True, 'method': 'GET'})
+ self.finish({'success': True, 'method': 'HEAD'})
+ self.finish({'success': True, 'method': 'POST'})
+ self.finish({'success': True, 'method': 'PUT'})
+ self.finish({'success': True, 'method': 'DELETE'})
+class MethodServer(tornado.web.Application):
+ handlers = [(r'/', MethodHandler)]
+ tornado.web.Application.__init__(self, handlers)
+ logging.info('Server started')
+ tornado.options.parse_command_line()
+ http_server = tornado.httpserver.HTTPServer(MethodServer())
+ http_server.listen(options.port)
+ tornado.ioloop.IOLoop.instance().start()
+if __name__ == '__main__':