Last active
May 29, 2025 12:59
-
-
Save Himura2la/197d87e034f51c04feb7d98b99a4c0c9 to your computer and use it in GitHub Desktop.
An alternative for undeservedly popular library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib.request import urlopen, Request | |
from urllib.error import HTTPError, URLError | |
def fetch(url, method=None, data=None, headers={}): | |
try: | |
with urlopen(Request( | |
url, | |
data=data.encode('ascii') if data else None, | |
headers=headers, | |
method=method | |
)) as resp: | |
resp_data = resp.read() | |
return resp_data.decode('utf-8') | |
except (HTTPError, URLError) as e: | |
print("Request failed:", e) | |
return None | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment