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

发布于 2024-10-12 11:33:59 字数 158 浏览 3 评论 0原文

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

定义 write 函数:

conn.setopt(pycurl.WRITEFUNCTION, on_receive)

感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-10-19 11:33:59

这是一个应该可以工作的简单版本。构建 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"

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文