Python CannotSendRequest

时间:2011-03-17 23:15:52

标签: python django

当我尝试连接到Twitter时,我在我的oauth库(Django socialauth)中收到了CannotSendRequest。

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.http_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: 

这是我正在创建HTTP连接的地方

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

我搜索了SO并找到了这些链接,但我仍然不确定如何实现该解决方案。我尝试过但都失败了。任何帮助将不胜感激。

httplib CannotSendRequest error in WSGI

When I use httplib for my OAUTH in Python, I always get "CannotSendRequest" and then "

1 个答案:

答案 0 :(得分:4)

The post you linked to表示当您重用已引发异常且未进入getresponse()阶段的连接时会发生此错误。

确实,connection.request("GET", "/") x 2会引发错误。

建议的解决方案是每次都重新创建连接。那是你想做的吗?注意我对此事没有意见,你刚刚问过如何实现这些帖子上的内容。

如果是这样,请删除connection()功能并始终执行

connection = httplib.HTTPSConnection(TWITTER_URL)
connection.request(req.http_method, req.to_url())
response = connection.getresponse().read()
connection.close()
return response