Rahul
Rahul

Reputation: 109

How to use the requests module to skip connection timeout urls

Hi how can i use the request module to go through a bunch of URLs and if a url in the list takes more time to load or a connection timeout how can i skip that particular url and skip to the next one

def req():
with open('demofile.txt','r') as http:
    for url in http.readlines():
        req = url.strip()
        print(req)
        page=requests.get("http://"+req,verify=False)
        if page.status_code == 400:
            break
        else:
            continue
        time.sleep(1)

Upvotes: 4

Views: 512

Answers (1)

Sufiyan Ghori
Sufiyan Ghori

Reputation: 18743

You can raise exception if there is a timeout and continue on finally block for next request,

import requests
import logging

timeout = 0.00001

try:
    response = requests.get(url="https://google.com", timeout=timeout)
except requests.exceptions.ConnectTimeout as e:
    logging.error("Time out!")
finally:
    # continue request here
    print("hello")


# output,
ERROR:root:Time out!
hello

Upvotes: 4

Related Questions