Friday, March 18, 2011

CannotSendRequest

The best way to see how the CannotSendRequest error works is to call request() multiple times:

http://stackoverflow.com/questions/1925639/httplib-cannotsendrequest-error-in-wsgi

>>> import httplib
>>> a = httplib.HTTPSConnection('api.twitter.com')
>>> a.request('GET', '/')
>>> a.request('GET', '/')
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.6/httplib.py", line 898, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.6/httplib.py", line 915, in _send_request
    self.putrequest(method, url, **skips)
  File "/usr/lib/python2.6/httplib.py", line 802, in putrequest
    raise CannotSendRequest()
httplib.CannotSendRequest

Then you can try:
>>> a.getresponse()

>>> a.request('GET', '/')
>>>

One thing observed is that Twitter seems to close the SSL connection after the 2nd time we ask for another request token, so we should avoid reusing the same HTTPS connection.

Also, if we reuse the connection a second time, BadStatus lines will start triggering because connection.getresponse() did not fully run and therefore does not sets the _CSIDLE state for the HTTPSConnection. As a result, no subsequent calls can be made. You may not see this issue as much on Apache running separate threads, but the issue is much more noticeable on Django's dev app server that runs on a single thread and the issue can easily be repeated.

Note: this problem does not happen with LinkedIn OAuth, so it seems specific to Twitter.

No comments:

Post a Comment