From: Christian Heller Date: Fri, 22 Apr 2016 23:23:54 +0000 (+0200) Subject: Force handle_url timeout by Unix signal. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7B%20web_path%20%7D%7D/blog?a=commitdiff_plain;h=f765b2b0cc73a27d98802f5e7ecb64a65b83ba4c;p=plomlombot-irc.git Force handle_url timeout by Unix signal. --- diff --git a/plomlombot.py b/plomlombot.py index 4d2f72d..741b01c 100755 --- a/plomlombot.py +++ b/plomlombot.py @@ -11,6 +11,7 @@ import bs4 import random import hashlib import os +import signal import plomsearch import irclog @@ -338,8 +339,16 @@ def handle_url(url, notice, show_url=False): handle_url(url, notice, True) return True + class TimeOut(Exception): + pass + + def timeout_handler(ignore1, ignore2): + raise TimeOut("timeout") + + signal.signal(signal.SIGALRM, timeout_handler) + signal.alarm(15) try: - r = requests.get(url, timeout=15, stream=True) + r = requests.get(url, stream=True) r.raw.decode_content = True text = r.raw.read(10000000+1) if len(text) > 10000000: @@ -347,12 +356,14 @@ def handle_url(url, notice, show_url=False): except (requests.exceptions.TooManyRedirects, requests.exceptions.ConnectionError, requests.exceptions.InvalidURL, - requests.exceptions.ReadTimeout, + TimeOut, UnicodeError, ValueError, requests.exceptions.InvalidSchema) as error: + signal.alarm(0) notice("TROUBLE FOLLOWING URL: " + str(error)) return False + signal.alarm(0) if mobile_twitter_hack(url): return True title = bs4.BeautifulSoup(text, "html5lib").title