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)

3

1 Answer 1

3

You can also use requests instead curl for handling response timeouts: http://docs.python-requests.org/en/master/user/quickstart/#timeouts. Something like this:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.