I am writing a Python script using pycurl
to consume the Twitter Sreaming API . Here is a small snippet that does just that (just enter your Twitter username / password to verify it):
import pycurl user = 'USER' password = 'PWD' def handleData(data): print(data) conn = pycurl.Curl() conn.setopt(pycurl.USERPWD, "%s:%s" % (user, password)) conn.setopt(pycurl.URL, 'https://stream.twitter.com/1/statuses/sample.json') conn.setopt(pycurl.WRITEFUNCTION, handleData) conn.perform()
The problem is that since the script consumes the thread, conn.perform()
never returns (or very rarely). Thus, I sometimes have to interrupt the script, and KeyboardInterrupt
with the perform()
method.
However, it does not cope with this, prints an ugly error, and raises another exception.
^CTraceback (most recent call last): File "test.py", line 6, in handleData def handleData(data): KeyboardInterrupt Traceback (most recent call last): File "test.py", line 12, in <module> conn.perform() pycurl.error: (23, 'Failed writing body (0 != 2203)')
The cURL FAQ says that to interrupt the current transfer, one of the callback functions (in my case handleData
) must return a special cost. This is great, but KeyboardInterrupt
doesn't fall into any of the callback functions!
How can I do this neatly?
EDIT: I know you can catch exceptions, but pycurl still does some funny things:
If I do this:
try: conn.perform() except BaseException as e: print('We caught the exception') print(type(e))
I get:
^CTraceback (most recent call last): File "test.py", line 6, in handleData def handleData(data): KeyboardInterrupt We caught the exception <class 'pycurl.error'>
This means that inside pycurl
performs some sort of catching function, prints an ugly error message, and then raises pycurl.error
.