14:26228d16d36a
Anton Shestakov <engored@ya.ru>, Sat, 15 Feb 2014 22:18:05 +0900
As the official md docs say, you can be lazy with blockquotes.

previous change 0:96660b4682ba

async-http-demo/client.py

Permissions: -rwxr-xr-x

Other formats: Feeds:
#!/usr/bin/env python
import re
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=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):
def __init__(self):
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):
def prepare(request):
body = self.span_re.findall(request.body.decode('utf-8'))[0]
callback(body)
self.http_client.fetch('http://pogoda.yandex.ru/moscow/', callback=prepare)
class WeatherHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
weather_client = WeatherClient()
weather_client.get(callback=self.callback)
def callback(self, body):
self.finish('<center>%s</center>' % body)
class AsyncClient(tornado.web.Application):
def __init__(self):
handlers = [
(r'/', WeatherHandler),
]
tornado.web.Application.__init__(self, handlers, debug=options.debug)
def main():
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__':
main()