11:251971044ff2
Anton Shestakov <engored@ya.ru>, Sat, 11 Jan 2014 23:57:14 +0900
More on qmonitor.

previous change 4:d783d7b930fe

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)
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(MethodClient())
http_server.listen(options.port)
logging.info('Client is starting on 127.0.0.1:{0}'.format(options.port))
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()