Files
Tobias Waldekranz af65bbe7f9 test: furl: Cap time spent waiting for HTTP requests
Without an explicit timeout, urllib will wait indefinitely for a
connection to be made.
2024-05-20 16:21:06 +02:00

25 lines
783 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
def nscheck(self, netns, needle):
""""Call check() from netns"""
return netns.call(lambda: self.check(needle))