Python 如何使用“请求”模块跳过连接超时URL

Python 如何使用“请求”模块跳过连接超时URL,python,python-3.x,python-requests,Python,Python 3.x,Python Requests,您好,我如何使用请求模块浏览一组url?如果列表中的url需要更多时间加载或连接超时,我如何跳过该特定url并跳到下一个 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)

您好,我如何使用请求模块浏览一组url?如果列表中的url需要更多时间加载或连接超时,我如何跳过该特定url并跳到下一个

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)

如果有超时,您可以引发异常,然后继续执行下一个请求的
finally

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