user4462740
user4462740

Reputation:

How to send one more request when failed for once with python-requests?

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!

Upvotes: 1

Views: 231

Answers (1)

Dušan Maďar
Dušan Maďar

Reputation: 9879

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)

Upvotes: 1

Related Questions