擅长:python、mysql、java
<p>您可以使用上下文管理器自动关闭它:</p>
<pre><code>import requests
with requests.Session() as s:
url = 'https://google.com'
r = s.get(url)
</code></pre>
<p>见<a href="https://2.python-requests.org/en/master/user/advanced/?highlight=session#session-objects" rel="nofollow noreferrer">requests docs > Sessions</a>:</p>
<blockquote>
<p>This will make sure the session is closed as soon as the with block is exited, even if unhandled exceptions occurred.</p>
</blockquote>