1

I am writing a url fetcher. When I send a request like:

import requests
response = requests.get("http://example.com")

Sometimes an error like this occurs:

ConnectionError: ('Connection aborted.', BadStatusLine(""''''")) 

But when I try one more time, it fixes. So I would like to send one more time when such an error occurs again. How can I do that? Thank you in advance!

1 Answers1

1

Repeat the request again when the exception is raised.

import requests

url = "http://example.com"

try:
    response = requests.get(url)
except requests.exception.ConnectionError:
    response = requests.get(url)
Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64