【问题标题】:Python3 http request close connection when nothing is sent没有发送任何内容时Python3 http请求关闭连接
【发布时间】:2019-07-02 23:16:00
【问题描述】:

我有一个具有以下解决方法的 API:

  1. 您发出 POST 请求,它返回“n”行数据:{json}
  2. 它将保持打开的连接至少 300 秒而不发送任何内容。

由于这非常慢,我想找到一种在不发送任何内容或在计时器之后关闭连接的方法。

【问题讨论】:

  • 你不能用timeout吗?

标签: python-3.x http python-requests


【解决方案1】:

所以,是的,这比我想象的要容易,我将使用http.client library 复制粘贴我的代码:

def asyncCall(url, data = None, timeout = 300,):
    conn = http.client.HTTPConnection(IP, timeout=timeout)
    conn.request("POST", url, bytes(json.dumps(data), encoding="utf-8"), )
    r1 = conn.getresponse()
    while not r1.closed:
        l = r1.readline().decode("utf-8")
        yield l

这样,它可以将每一行代码传递给回调(在单独的Process 中运行)并在timeout 之后关闭连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-08
    • 2020-06-07
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多