Files
infix/test/infamy/furl.py
T
Tobias Waldekranz 77215aeb87 infamy: furl: Handle connection resets
Furl answers the question "is this URL serving the content I expect?"

Sometimes, we might be asking a server that in the middle of being
reconfigured, is crashing, is buggy, etc. - in which case it might RST
the connection. In that case the answer to the question is "False" and
not "BURN IT ALL DOWN!!"
2024-12-19 09:33:45 +01:00

27 lines
845 B
Python

"""Fugly URL fetcher"""
import urllib.error
import urllib.request
class Furl:
"""Furl wraps urllib in a way similar to curl"""
def __init__(self, url):
"""Create new URL checker"""
self.url = urllib.parse.quote(url, safe='/:')
def check(self, needle, timeout=10):
"""Connect to web server URL, fetch body and check for needle"""
try:
with urllib.request.urlopen(self.url, timeout=timeout) as response:
text = response.read().decode('utf-8')
#print(text)
return needle in text
except urllib.error.URLError as _:
return False
except ConnectionResetError:
return False
def nscheck(self, netns, needle):
""""Call check() from netns"""
return netns.call(lambda: self.check(needle))