python WSGI 中的 httplib 无法发送请求错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1925639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:22:10  来源:igfitidea点击:

httplib CannotSendRequest error in WSGI

pythontwitteroauthwsgihttplib

提问by Dave

I've used two different python oauth libraries with Django to authenticate with twitter. The setup is on apache with WSGI. When I restart the server everything works great for about 10 minutes and then the httplib seems to lock up (see the following error).

我在 Django 中使用了两个不同的 python oauth 库来对 twitter 进行身份验证。设置在带有 WSGI 的 apache 上。当我重新启动服务器时,一切正常运行大约 10 分钟,然后 httplib 似乎锁定(请参阅以下错误)。

I'm running only 1 process and 1 thread of WSGI but that seems to make no difference.

我只运行 WSGI 的 1 个进程和 1 个线程,但这似乎没有区别。

I cannot figure out why it's locking up and giving this CannotSendRequest error. I've spent a lot of hours on this frustrating problem. Any hints/suggestions of what it could be would be greatly appreciated.

我无法弄清楚为什么它会锁定并给出此无法发送请求错误。我在这个令人沮丧的问题上花了很多时间。任何关于它可能是什么的提示/建议将不胜感激。

File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 92, in get_response
  response = callback(request, *callback_args, **callback_kwargs)

File "mypath/auth/decorators.py", line 9, in decorated
  return f(*args, **kwargs)

File "mypath/auth/views.py", line 30, in login
  token = get_unauthorized_token()

File "/root/storm/eye/auth/utils.py", line 49, in get_unauthorized_token
  return oauth.OAuthToken.from_string(oauth_response(req))

File "mypath/auth/utils.py", line 41, in oauth_response
  connection().request(req.http_method, req.to_url())

File "/usr/lib/python2.5/httplib.py", line 866, in request
  self._send_request(method, url, body, headers)

File "/usr/lib/python2.5/httplib.py", line 883, in _send_request
  self.putrequest(method, url, **skips)

File "/usr/lib/python2.5/httplib.py", line 770, in putrequest
  raise CannotSendRequest()

CannotSendRequest

无法发送请求

回答by Denis Otkidach

This exception is raised when you reuse httplib.HTTPobject for new request while you havn't called its getresponse()method for previous request. Probably there was some other error before this one that left connection in broken state. The simplest reliable way to fix the problem is creating new connection for each request, not reusing it. Sure, it will be a bit slower, but I think it's not an issue having you are running application in single process and single thread.

当您httplib.HTTP为新请求重用对象而您尚未getresponse()为先前请求调用其方法时,会引发此异常。在此之前可能还有其他一些错误使连接处于断开状态。解决问题的最简单可靠的方法是为每个请求创建新连接,而不是重用它。当然,它会慢一点,但我认为在单进程和单线程中运行应用程序不是问题。

回答by HelloWorld

Also check your Python version. I had a similar situation after updating to Py-2.7 from Py-2.6. In Py-2.6 everything worked without any problems. Py-2.7 httplib uses HTTP/1.1 by default which made the server did not send back the Connection: closeoption in the respond, therefore the connection handling was broken. In my case this worked with HTTP/1.0 though.

还要检查您的 Python 版本。从 Py-2.6 更新到 Py-2.7 后,我遇到了类似的情况。在 Py-2.6 中,一切正常,没有任何问题。Py-2.7 httplib 默认使用 HTTP/1.1,这使得服务器没有在响应中发回Connection: close选项,因此连接处理被破坏。就我而言,这适用于 HTTP/1.0。