set timeout in request python code example
Example 1: python requests.get timeout
import requests
link = 'https://google.com'
request_from_link = requests.get(link, timeout=10)
print(request_from_link)
Example 2: python timeout
You may use the signal package if you are running on UNIX:
import signal
def handler(signum, frame):
print("Forever is over!")
raise Exception("end of time")
def loop_forever():
import time
while 1:
print("sec")
time.sleep(1)
signal.signal(signal.SIGALRM, handler)
signal.alarm(10)
0
try:
loop_forever()
except Exception, exc:
print(exc)