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实现异步请求
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- ASIHttpRequest 是一款极其强劲的HTTP访问开源项目,让简单的API完成复杂的功能, 如:异步请求...
- 1、同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操...