How to implement a retry option in PyCurl

1.5k views Asked by At

How could I implement a retry option within the PyCurl (libcurl) module in python? Something similar to the effect of:

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

Current code:

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()
1

There are 1 answers

0
Jan Dvorak On

Pycurl doesn't know how to re-initialize the consumer you supply through the WRITEDATA or WRITEFUNCTION options, so your code has to implement the retry logic:

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