我知道 requests.get()
提供了一个 HTTP 接口(interface),以便程序员可以向 HTTP 服务器发出各种请求。
这告诉我必须在某处打开一个端 Eloquent 能发生请求。
考虑到这一点,如果脚本在响应/完成请求之前停止(例如,通过键盘中断,因此执行脚本的机器保持连接到互联网)会发生什么情况?
端口/连接是否保持打开状态?
端口/连接是否自动关闭?
最佳答案
问题的简短回答是:请求将在任何异常情况下关闭连接,包括KeyboardInterrupt
和SystemExit
。
A little digging进入请求源代码显示 requests.get
最终调用了 HTTPAdapter.send
方法(这是所有魔法发生的地方)。
在 send
方法中可以通过两种方式发出请求:chunked 或 not chunked。我们执行哪个发送
取决于request.body
和Content-Length
header 的值:
chunked = not (request.body is None or 'Content-Length' in request.headers)
在请求主体为None
或设置了Content-Length
的情况下,requests
将为make use高层urlopen
urllib3
的方法:
if not chunked:
resp = conn.urlopen(
method=request.method,
url=url,
body=request.body,
# ...
)
finally
urllib3.PoolManager.urlopen
方法的代码块包含在 try
block 未成功执行的情况下处理关闭连接的代码:
clean_exit = False
# ...
try:
# ...
# Everything went great!
clean_exit = True
finally:
if not clean_exit:
# We hit some kind of exception, handled or otherwise. We need
# to throw the connection away unless explicitly told not to.
# Close the connection, set the variable to None, and make sure
# we put the None back in the pool to avoid leaking it.
conn = conn and conn.close()
release_this_conn = True
在响应可以分 block 的情况下,请求会进入较低级别并使用 urllib3
提供的底层低级别连接。在这种情况下,requests 仍然处理异常,它使用 try
/ except
执行此操作在获取连接后立即开始的 block ,并以以下方式结束:
low_conn = conn._get_conn(timeout=DEFAULT_POOL_TIMEOUT)
try:
# ...
except:
# If we hit any problems here, clean up the connection.
# Then, reraise so that we can handle the actual exception.
low_conn.close()
raise
有趣的是,如果没有错误,连接可能不会关闭,这取决于您为 urllib3
配置连接池的方式。在成功执行的情况下,连接被放回连接池(虽然我在 requests
源中找不到 _put_conn
调用用于分 block send
,这可能是分 block 工作流程中的错误)。
关于Python:如果脚本在 requests.get() 执行时停止会怎样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51831726/