0

我在 Tornado 中有这部分代码:

............
como_url = "".join(['http://', options.como_address, ':', options.como_port, 
                        '/ztc_config?netid=0&opcode_group=0&opcode=0&start=-20s&end=-1s'])

http_client = AsyncHTTPClient()
response = yield tornado.gen.Task(http_client.fetch, como_url)

ret = {}
if response.error:
    ret['error'] = 'Error while retrieving the response'
    self.write(tornado.escape.json_encode(ret))
    self.finish()
else:
.........

我向传感器发送命令,然后向数据库发出 http 请求以检索传感器的响应。在我做出回应并采取一些行动之后。

现在,我必须发送两次 http 请求来检索响应。我第一次向传感器发送命令时,请求太快而无法接收响应。

重新发送命令后,发现响应正确,一切正常。

为什么?如何设置我不知道请求的超时...或者如何在 Tornado 中重新发送请求两次?

谢谢你。

编辑

我是这样写的:

como_url = "".join(['http://', options.como_address, ':', options.como_port, 
                        '/ztc_config?netid=0&opcode_group=0&opcode=0&start=-20s&end=-1s'])

    http_client = AsyncHTTPClient()
    #response = yield tornado.gen.Task(http_client.fetch, como_url)
    request = tornado.httpclient.HTTPRequest(url=como_url, connect_timeout=5.0, request_timeout=2.0)
    response = yield tornado.gen.Task(http_client.fetch, request)
    print response

我得到:

HTTPResponse(code=200,request_time=0.30609703063964844,buffer=<io.BytesIO object at 0x276a1d0>,_body=None,time_info={},request=<tornado.httpclient.HTTPRequest object at 0x2764310>,effective_url='http://131.114.52.207:44444/ztc_config?netid=0&opcode_group=0&opcode=0&start=-20s&end=-1s',headers={'Content-Type': 'text/plain'},error=None)

为什么request_time=0???

4

0 回答 0