我在尝试通过HTTP检索资源时遇到了一个非常奇怪的错误。它似乎发生在任何HTTP客户端(尝试请求和urllib具有相同的结果)。使用Python的HTTP请求:TypeError:需要一个整数(得到类型套接字)
我的项目使用django,我使用tox和标准django命令python manage.py test
运行我的测试。当我运行我的测试套件并且单元测试发出HTTP请求(例如通过requests.get('http://example.com')
)时,测试失败并出现错误,测试套件一直持续到结束并挂起。我必须通过命令行手动杀死进程。
一些调查后,我把try/except
块arount http请求,并得到了以下堆栈跟踪:
File "/mycomputer/python3.4/site-packages/requests/api.py", line 68, in get
return request('get', url, **kwargs)
File "/mycomputer/python3.4/site-packages/requests/api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "/mycomputer/python3.4/site-packages/requests/sessions.py", line 464, in request
resp = self.send(prep, **send_kwargs)
File "/mycomputer/python3.4/site-packages/requests/sessions.py", line 576, in send
r = adapter.send(request, **kwargs)
File "/mycomputer/python3.4/site-packages/requests/adapters.py", line 370, in send
timeout=timeout
File "/mycomputer/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "/mycomputer/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 349, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.4/http/client.py", line 1065, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.4/http/client.py", line 1103, in _send_request
self.endheaders(body)
File "/usr/lib/python3.4/http/client.py", line 1061, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.4/http/client.py", line 906, in _send_output
self.send(msg)
File "/usr/lib/python3.4/http/client.py", line 841, in send
self.connect()
File "/mycomputer/python3.4/site-packages/requests/packages/urllib3/connection.py", line 155, in connect
conn = self._new_conn()
File "/mycomputer/python3.4/site-packages/requests/packages/urllib3/connection.py", line 134, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "/mycomputer/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 68, in create_connection
sock = socket.socket(af, socktype, proto)
File "/usr/lib/python3.4/socket.py", line 123, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
TypeError: an integer is required (got type socket)
我真的不明白这里的问题。从命令行运行相同的东西完美地工作,所以它可能与我的项目体系结构有关。另外,在另一台计算机上运行测试套件也会以同样的方式失败。
有没有人遇到过类似的问题?我能做些什么来追踪问题?
对不起,如果我不清楚:我没有对我自己的网站做请求。所有请求都是针对第三方API完成的。所以我不认为它与django的内置web服务器有关。 –