简体   繁体   English

Python的HTTP请求:TypeError:需要一个整数(获取类型套接字)

[英]HTTP request with Python : TypeError: an integer is required (got type socket)

I'm facing a really strange bug while trying to retrieve a resource via HTTP. 在尝试通过HTTP检索资源时,我遇到了一个非常奇怪的错误。 It seems to happen with any HTTP client (tried requests and urllib with the same results). 它似乎发生在任何HTTP客户端(尝试过的请求和urllib具有相同的结果)。

My project uses django, and I run my tests using tox and the standard django command python manage.py test . 我的项目使用django,我使用tox和标准的django命令运行我的测试python manage.py test When I run my test suite and a unit test makes a HTTP request (eg via requests.get('http://example.com') ), the tests fails with an error, the test suite continue until the end, and hangs. 当我运行我的测试套件并且单元测试发出HTTP请求时(例如通过requests.get('http://example.com') ),测试失败并出现错误,测试套件一直持续到最后,然后挂起。 I have to manually kill the process via command line. 我必须通过命令行手动终止进程。

After some investigations, I put a try / except block arount the http request, and got the following stacktrace: 经过一些调查,我在一个http请求中放了一个try / except块,得到了以下的栈跟踪:

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)

I really don't understand the problem here. 我真的不明白这里的问题。 Running the same thing from the command line works perfectly, so it's probably related to my project architecture. 从命令行运行相同的东西是完美的,所以它可能与我的项目架构有关。 Also, running the test suite on another computer fails the same way. 此外,在另一台计算机上运行测试套件失败的方式相同。

Have anybody meet a similar issue ? 有没有人遇到过类似的问题? What can I do do track down the problem ? 我该怎么做才能找到问题?

好的,问题是由HTTPretty引起的, HTTPretty是我用来模拟的第三方软件包。

Somewhere I've read that django is not a full server and can cause problems when you try to use it as one. 在某个地方我读过django不是一个完整的服务器,当你试图将它作为一个服务器使用它时会引起问题。 Perhaps this is one of this cases. 也许这是其中一个案例。

Did you try https://docs.djangoproject.com/en/1.8/topics/testing/advanced/ ? 你试过https://docs.djangoproject.com/en/1.8/topics/testing/advanced/吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python的HTTP请求:TypeError:必需为整数(获取类型dict) - HTTP request with Python : TypeError: an integer is required (got type dict) Python TypeError:整数是必需的(got类型列表) - Python TypeError: an integer is required (got type list) Python:类型错误:需要一个整数(得到类型 str) - Python: TypeError: an integer is required (got type str) TypeError:Python中需要一个整数(类型为str) - TypeError: an integer is required (got type str) in python Python: TypeError: an integer is required (got type module) - Python: TypeError: an integer is required (got type module) Python:TypeError 需要 integer(获取类型 str) - Python: TypeError an integer is required(got type str) Python 2-如何修复TypeError:必须为整数(类型为str) - Python 2 - How to fix TypeError: an integer is required (got type str) Python多处理日志记录错误-TypeError:必须为整数(获取类型为NoneType) - Python Multiprocessing Logging Error - TypeError: an integer is required (got type NoneType) TypeError:需要一个 integer(获取类型元组)<python><opencv><tesseract></tesseract></opencv></python> - TypeError: an integer is required (got type tuple) <python> <OpenCV> <tesseract> Python - 类型错误:需要一个 integer(获取类型 datetime.datetime) - Python - TypeError: an integer is required (got type datetime.datetime)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM