【问题标题】:How to implement a retry option in PyCurl如何在 PyCurl 中实现重试选项
【发布时间】:2016-12-23 16:34:27
【问题描述】:

如何在 python 的 PyCurl (libcurl) 模块中实现重试选项?类似于以下效果的东西:

curl --retry 3 --retry-delay 5 'http://somesite.com/somefile'

当前代码:

buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://somesite.com/somefile')
with open('output.txt','w') as f:
  c.setopt(c.WRITEFUNCTION, f.write)
  c.perform()

【问题讨论】:

    标签: python curl libcurl pycurl


    【解决方案1】:

    Pycurl 不知道如何重新初始化您通过 WRITEDATAWRITEFUNCTION 选项提供的消费者,因此您的代码必须实现重试逻辑:

    retries_left = 3
    delay_between_retries = 5 # seconds
    success = False
    c = pycurl.Curl()
    c.setopt(c.URL, 'http://somesite.com/somefile')
    while retries_left > 0:
      try:
        with open('output.txt', 'w') as f:
          c.setopt(c.WRITEFUNCTION, f.write)
          c.perform()
        success = True
        break
      except BaseException as e:
        retries_left -= 1
        time.sleep(delay_between_retries)
    # check success
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 2013-01-29
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      相关资源
      最近更新 更多