Search code examples
pythoncurlpycurl

How can I limit the number of curl calls to the write function?


I'm trying to limit the number of times this WRITEFUNCTION is called. Is there any way I can do that?

defining the writefunction:

conn.setopt(pycurl.WRITEFUNCTION, on_receive)

Thanks for the help!


Solution

  • Here's a dirty simple version that should work. Building PycURL to test and find a better way.

    import pycurl, json
    
    STREAM_URL = "http://chirpstream.twitter.com/2b/user.json"
    
    USER = "segphault"
    PASS = "XXXXXXXXX"
    
    class LimitError(Exception): pass
    
    counter = 0
    limit = 10
    def on_receive(data):
        global counter
        if counter < 10:
            print data
            counter += 1
        else:
            raise LimitError    
    conn = pycurl.Curl()
    conn.setopt(pycurl.USERPWD, "%s:%s" % (USER, PASS))
    conn.setopt(pycurl.URL, STREAM_URL)
    conn.setopt(pycurl.WRITEFUNCTION, on_receive)
    
    try:
        conn.perform()
        print "Exited Normally"
    except LimitError:
        print "Reached limit, exiting"
    except pycurl.error:
        if counter == limit:
            print "pycurl expected error, nothing to worry about"
        else:
            raise
    finally:
        conn.close()
    
    print "All done"