Wednesday, January 19, 2011

M2Crypto and Facebook's SDK hangs...

Another issue is that even if I fixed the issue, the Facebook SDK still hangs when trying to read a response with urllib.urlopen() and M2Crypto::
import M2Crypto
import urllib
print urllib.urlopen(("https://graph.facebook.com/me?" + urllib.urlencode({'access_token': [your access token here]'}).read()

...but this works:
import M2Crypto
import urllib

urllib._urlopener = urllib.FancyURLopener()
urllib._urlopener.addheader('Connection', 'close')
u = urllib.urlopen("https://graph.facebook.com/me?" + urllib.urlencode({'access_token': [your access token here]'}))
data = u.read()
print data

The urllib._urlopener code is essentially what is done by urllib.urlopen(). The major difference is that we add a Connection: close header.

If the Facebook SDK code would just change to use urllib2 instead of urllib2, the issue goes away (most likely because M2Crypto hasn't been hijacking the code). The urllib2 automatically adds a Connection: close header (see http://docs.python.org/library/urllib2.html)
import M2Crypto
import urllib
print urllib2.urlopen(("https://graph.facebook.com/me?" + urllib.urlencode({'access_token': [your access token here]'}).read()

2 comments:

  1. Any idea when this behavior began? I first started seeing it today. Thanks for the tip!

    ReplyDelete
  2. I just started seeing the issue after we started to use M2Crypto (or rather importing Google's Gdata code). I haven't dug around the M2Crypto code to figure out how its behavior is different than that of standard urllib library, but it appears to keep the socket open and not return back data until the connection is closed (if you specify the # of bytes to read, then the # of bytes are returned).

    ReplyDelete