Skip to content

Instantly share code, notes, and snippets.

@Himura2la
Last active April 3, 2024 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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, HTTPError
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 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