0:96660b4682ba
Anton Shestakov <engored@ya.ru>, Sat, 12 Jan 2013 15:36:36 +0900
New repo for experiments. First two occupants: async-http-demo and http-methods-demo.

next change 3:195699406124

http-methods-demo/client.py

Permissions: -rwxr-xr-x

Other formats: Feeds:
#!/usr/bin/env python
import logging
import tornado.httpserver
import tornado.httpclient
import tornado.ioloop
import tornado.options
import tornado.web
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
def get(self):
http_client = tornado.httpclient.AsyncHTTPClient()
if method in ('POST', 'PUT'):
body = 'I make simple_httpclient happy :)'
else:
body = None
http_client.fetch(
'http://localhost:%i/' % options.server_port,
callback=self.callback,
method=method,
body=body
)
def callback(self, response):
self.finish(response.body)
return ResultingHandler
class MethodClient(tornado.web.Application):
def __init__(self):
handlers = [
(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')
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(MethodClient())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()