Python CannotSendRequest
I receive a CannotSendRequest in my oauth library (Django socialauth) when I try to connect to Twitter.
Traceback:
File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/me/webfaction/project/socialauth/views.py" in twitter_login
94. request_token = twitter.fetch_request_token(callback=request.build_absolute_uri(reverse('socialauth_twitter_login_done')))
File "/Users/me/webfaction/project/socialauth/lib/oauthtwitter2.py" in fetch_request_token
50. return oauth.OAuthToken.from_string(oauth_response(oauth_request))
File "/Users/me/webfaction/project/socialauth/lib/oauthtwitter2.py" in oauth_response
33. connection().request(req.h开发者_如何学JAVAttp_method, req.to_url())
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py" in request
914. self._send_request(method, url, body, headers)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py" in _send_request
931. self.putrequest(method, url, **skips)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py" in putrequest
818. raise CannotSendRequest()
Exception Type: CannotSendRequest at /accounts/twitter_login/
Exception Value:
Here's where I'm creating the HTTP connection
def connection():
try:
return connection._connection
except AttributeError:
connection._connection = httplib.HTTPSConnection(TWITTER_URL)
return connection._connection
def oauth_response(req):
connection().request(req.http_method, req.to_url())
return connection().getresponse().read()
I've searched SO and found these links, but I'm still not sure how to implement the solution. I've tried and failed. Any help would be appreciated.
httplib CannotSendRequest error in WSGI
When I use httplib for my OAUTH in Python, I always get "CannotSendRequest" and then "
The post you linked to says this error happens when you reuse connections that have thrown an exception and didn't make it to the getresponse() stage.
Indeed, connection.request("GET", "/")
x 2 throws the error.
The solution suggested is to re-create the connection every time. Is that what you want to do? Note I have no opinions on this matter, you just asked how to implement what's on those posts.
If so, get rid of your connection()
function and always do
connection = httplib.HTTPSConnection(TWITTER_URL)
connection.request(req.http_method, req.to_url())
response = connection.getresponse().read()
connection.close()
return response
精彩评论