Skip to content

Instantly share code, notes, and snippets.

@Himura2la
Last active May 29, 2025 12:59
Show Gist options
  • Save Himura2la/197d87e034f51c04feb7d98b99a4c0c9 to your computer and use it in GitHub Desktop.
Save Himura2la/197d87e034f51c04feb7d98b99a4c0c9 to your computer and use it in GitHub Desktop.
An alternative for undeservedly popular library
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