Tornado实例
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.httpclient
import tornado.gen
import json
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
body = json.dumps({'phone': '13818617241','passwd':'666666'})
client = tornado.httpclient.AsyncHTTPClient()
response = yield tornado.gen.Task(
client.fetch,
'http://api.****.com/*****/user/login',#替换有效url
method='POST',
body=body,
)
self.write(response.body)
self.finish()
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[
(r"/", IndexHandler)
]
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
Flask实例:
需要gevent框架配合
import time
from flask import *
from gevent import monkey
from gevent.pywsgi import WSGIServer
monkey.patch_all()
app = Flask(__name__)
@app.route('/')
def helloworld():
time.sleep(10)
return '睡眠10秒才出现'
@app.route('/home')
def hellohome():
return '异步立刻出现'
if __name__ == '__main__':
http_server = WSGIServer(('0.0.0.0', 8000), app)
http_server.serve_forever()
网友评论