在 Python 3 中使用 urllib 的 socket ResourceWarning [英] socket ResourceWarning using urllib in Python 3

查看:25
本文介绍了在 Python 3 中使用 urllib 的 socket ResourceWarning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 urllib.request.urlopen() 从我要测试的网络服务中获取数据.

I am using a urllib.request.urlopen() to GET from a web service I'm trying to test.

这将返回一个 HTTPResponse 对象,然后我 read() 以获取响应正文.

This returns an HTTPResponse object, which I then read() to get the response body.

但我总是从 socket.py 看到一个关于未关闭套接字的 ResourceWarning

But I always see a ResourceWarning about an unclosed socket from socket.py

相关函数如下:

from urllib.request import Request, urlopen

def get_from_webservice(url):
    """ GET from the webservice  """
    req = Request(url, method="GET", headers=HEADERS)
    with urlopen(req) as rsp:
        body = rsp.read().decode('utf-8')
        return json.loads(body)

这是出现在程序输出中的警告:

Here's the warning as it appears in the program's output:

$ ./test/test_webservices.py
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py:359: ResourceWarning: unclosed <socket.socket object, fd=5, family=30, type=1, proto=6>
self._sock = None
.s
----------------------------------------------------------------------
Ran 2 tests in 0.010s

OK (skipped=1)

如果我可以对 HTTPResponse(或请求?)做些什么来让它干净地关闭它的套接字,我真的很想知道,因为这段代码用于我的单元测试;我不喜欢忽略任何地方的警告,尤其是不在那里.

If there's anything I can do to the HTTPResponse (or the Request?) to make it close its socket cleanly, I would really like to know, because this code is for my unit tests; I don't like ignoring warnings anywhere, but especially not there.

推荐答案

我不知道这是否是答案,但它是答案的一部分.

I don't know if this is the answer, but it is part of the way to an answer.

如果我将标头connection: close"添加到来自我的 Web 服务的响应中,HTTPResponse 对象似乎会在没有警告的情况下正确清理自己.

If I add the header "connection: close" to the response from my web services, the HTTPResponse object seems to clean itself up properly without a warning.

事实上,HTTP 规范 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) 说:

And in fact, the HTTP Spec (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) says:

不支持持久连接的 HTTP/1.1 应用程序必须在每条消息中包含关闭"连接选项.

HTTP/1.1 applications that do not support persistent connections MUST include the "close" connection option in every message.

所以问题出在服务器端(即我的错!).如果您无法控制来自服务器的标头,我不知道您能做什么.

So the problem was on the server end (i.e. my fault!). In the event that you don't have control over the headers coming from the server, I don't know what you can do.

这篇关于在 Python 3 中使用 urllib 的 socket ResourceWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆