繁体   English   中英

如何限制对write函数的curl调用次数?

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

我试图限制此WRITEFUNCTION被调用的次数。 有什么办法可以做到吗?

定义writefunction:

conn.setopt(pycurl.WRITEFUNCTION, on_receive)

谢谢您的帮助!

这是一个应该工作的肮脏的简单版本。 构建PycURL进行测试并找到更好的方法。

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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM