美文网首页
tornado实现异步请求

tornado实现异步请求

作者: 不懒狮Blaise | 来源:发表于2017-08-28 23:00 被阅读0次
from tornado.web import FallbackHandler, RequestHandler, Application, asynchronous
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.escape import json_encode, json_decode
from tornado.httpclient import AsyncHTTPClient, HTTPRequest, HTTPClient
import functools
from flask import Flask


app = Flask(__name__)
class TorTest(RequestHandler):

    def write(self, truck):
        RequestHandler.write(self, truck)


    @asynchronous
    def post(self):
        ptlogin_url = "http://127.0.0.1:5000/hello"
        pt_body = json_encode({
                "appid": 'abcdefg',
                "sthirdparty": channel,
                "sid": sid,
                })
        
        http_client = AsyncHTTPClient()
        request = HTTPRequest(ptlogin_url, 
                              method="POST", 
                              headers={'Content-Type': 'text/plain', 'Content-Length': len(pt_body)},
                              body=pt_body
                              )
        
        http_client.fetch(request, functools.partial(self.on_ptlogin, pt_body))
        print 'post'
        self.finish()

    def on_ptlogin(self, pt_body, responseorg):
        
        self.write(pt_body)
        self.finish()
        return


@app.route('/hello', methods=['GET','POST'])
def hello():
    print 'hello'

tr = WSGIContainer(app)


application = Application([
    (r"/TorTest", TorTest),
    (r".*", FallbackHandler, dict(fallback=tr)),
])



http_server = HTTPServer(application, xheaders=True)
http_server.listen(5000)
IOLoop.instance().start()

相关文章

网友评论

      本文标题:tornado实现异步请求

      本文链接:https://www.haomeiwen.com/subject/kldndxtx.html