gpt4 book ai didi

python - 如何在没有服务器的情况下立即执行 AsyncHTTPClient() (使用交互式解释器)

转载 作者:行者123 更新时间:2023-11-30 23:33:26 25 4
gpt4 key购买 nike

我输入了AsyncHTTPClient sample code来自tornado网站的交互式Python解释器,但异步HTTP请求从未执行。

def handle_request(response):
if response.error:
print "Error:", response.error
else:
print response.body

http_client = AsyncHTTPClient()
http_client.fetch("http://www.google.com/", handle_request)

# handle_request function is never executed (nothing is printed)

我可以不使用 AsyncHTTPClient 作为 Web 服务器处理的一部分吗?

最佳答案

是的,但您应该启动 IOLoop ,例如 docs :

from tornado import ioloop
from tornado.httpclient import AsyncHTTPClient


def handle_request(response):
if response.error:
print "Error:", response.error
else:
print response.body
ioloop.IOLoop.instance().stop()

http_client = AsyncHTTPClient()
http_client.fetch("http://www.google.com/", handle_request)
ioloop.IOLoop.instance().start()

关于python - 如何在没有服务器的情况下立即执行 AsyncHTTPClient() (使用交互式解释器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18801879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com