Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

python - httplib CannotSendRequest error in WSGI

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).

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This exception is raised when you reuse httplib.HTTP object 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...