add timeouts to all HTTP requests to avoid hangs

This commit is contained in:
rimu 2024-07-30 11:18:06 +08:00
parent da69a87978
commit 1a2203038c
2 changed files with 4 additions and 3 deletions

View file

@ -83,7 +83,7 @@ class Recaptcha3Validator(object):
'response': response 'response': response
} }
http_response = requests.post(RECAPTCHA_VERIFY_SERVER, data) http_response = requests.post(RECAPTCHA_VERIFY_SERVER, data, timeout=10)
if http_response.status_code != 200: if http_response.status_code != 200:
return False return False

View file

@ -204,7 +204,7 @@ def is_video_url(url):
def mime_type_using_head(url): def mime_type_using_head(url):
# Find the mime type of a url by doing a HEAD request - this is the same as GET except only the HTTP headers are transferred # Find the mime type of a url by doing a HEAD request - this is the same as GET except only the HTTP headers are transferred
try: try:
response = requests.head(url) response = requests.head(url, timeout=5)
response.raise_for_status() # Raise an exception for HTTP errors response.raise_for_status() # Raise an exception for HTTP errors
content_type = response.headers.get('Content-Type') content_type = response.headers.get('Content-Type')
if content_type: if content_type:
@ -1053,7 +1053,8 @@ def in_sorted_list(arr, target):
# Makes a still image from a video url, without downloading the whole video file # Makes a still image from a video url, without downloading the whole video file
def generate_image_from_video_url(video_url, output_path, length=2): def generate_image_from_video_url(video_url, output_path, length=2):
response = requests.get(video_url, stream=True, headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0'}) # Imgur requires a user agent response = requests.get(video_url, stream=True, timeout=5,
headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:127.0) Gecko/20100101 Firefox/127.0'}) # Imgur requires a user agent
content_type = response.headers.get('Content-Type') content_type = response.headers.get('Content-Type')
if content_type: if content_type:
if 'video/mp4' in content_type: if 'video/mp4' in content_type: