1
import commands
    f = open("test.txt","r").readlines()    
    for l in f:
            x = l.strip()
            url = ("https://"+x+"/test")
            c = commands.getoutput("curl -I "+url)
            print (c)

When the code is executed, the code takes a long time in this line [c = commands.getoutput("curl -I "+url)], I want to set a time for example 5 seconds. If it is longer than 5 seconds, move to the next line in the9 for loop)

4

1 回答 1

3

您也可以使用requestscurl 来处理响应超时: http ://docs.python-requests.org/en/master/user/quickstart/#timeouts 。像这样的东西:

import requests
from requests.exceptions import Timeout

import commands

f = open("test.txt","r").readlines()    
for l in f:
    x = l.strip()
    url = ("https://"+x+"/test")
    try:
        response = requests.get(url, timeout=5)
    except Timeout:
        # do something
        continue
于 2018-09-06T16:05:00.823 回答