Skip to content

Instantly share code, notes, and snippets.

@egdoc
Last active April 16, 2019 07:48
Show Gist options
  • Select an option

  • Save egdoc/4dfd441f83a7e298efa6721662248e3f to your computer and use it in GitHub Desktop.

Select an option

Save egdoc/4dfd441f83a7e298efa6721662248e3f to your computer and use it in GitHub Desktop.
Python HTTP requests
#!/usr/bin/env python3
import requests
# steam must be True, otherwise the content will be downloaded all at once
with requests.get("https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.7.tar.xz", stream=True) as response:
with open("latest-kernel.tar.xz", "wb") as tarball:
# iter_content method iterates over response content
# the argument is chunk_size
for chunk in response.iter_content(16384):
tarball.write(chunk)
#!/usr/bin/env python3
from urllib.request import urlopen
with urlopen("https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.7.tar.xz") as response:
with open('latest-kernel.tar.xz', 'wb') as tarball:
while True:
# Read chunks of 16 Kibibytes
chunk = response.read(16384)
if chunk:
tarball.write(chunk)
else:
break
#!/usr/bin/env python3
from shutil import copyfileobj
from urllib.request import urlopen
with urlopen("https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.7.tar.xz") as response:
with open('latest-kernel.tar.xz', 'wb') as tarball:
# Third argument of shutil.copyfileobject is buffer size,
# which by default is set to 16384 bytes
copyfileobj(response, tarball, length=16384)
#!/usr/bin/env python3
from urllib.request import urlretrieve
# urlretrieve function might become deprecated
filename, message_obj = urlretrieve("https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.7.tar.xz", 'linux.tar.xz')
#!/usr/bin/env python3
import requests
session = requests.Session()
# Send GET request from session context
response = session.get("https://httpbin.org/cookies/set?lastname=skywalker")
# Build, prepare and send a Request manually
request = requests.Request("GET", "https://httpbin.org/get")
prepared_request = session.prepare_request(request)
response = session.send(prepared_request)
#!/usr/bin/env python3
from urllib.parse import urlencode
from urllib.request import Request, urlopen
# Simplest GET request
with urlopen("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY") as response:
# Do something with the response e.g reading the content as json
response_body = response.read()
# GET request with "prepared" query string
query_string = urlencode({"api_key": "DEMO_KEY", "date": "2019-04-11"})
with urlopen("?".join(["https://api.nasa.gov/planetary/apod", query_string])) as response:
response_body = response.read()
#!/usr/bin/env python3
import json
from urllib.request import Request, urlopen
# Example of PUT request with data: use the "method" argument
person = {"firstname": "Luke", "lastname": "Skywalker", "title": "Jedi Knight"}
custom_headers = {"Content-Type": "application/json"}
request = Request("https://httpbin.org/put", json.dumps(person).encode("ascii"), headers=custom_headers, method="PUT")
with urlopen(request) as response:
response_body = response.read()
#!/usr/bin/env python3
import json
from urllib.parse import urlencode
from urllib.request import Request, urlopen
# POST request with form data: The ascii-encoded data must be the second parameter passed
# to the urlopen function. If the data argument is not None, the POST verb is used (default is GET)
data = urlencode({"variable1": "value1", "variable2": "value2"}).encode('ascii')
with urlopen("https://httpbin.org/post", data) as response:
response_body = response.read()
# POST request with Json data
person = {"firstname": "Luke", "lastname": "Skywalker", "title": "Jedi Knight"}
custom_headers = {"Content-Type": "application/json"}
request = Request("https://httpbin.org/post", json.dumps(person).encode("ascii"), headers=custom_headers)
with urlopen(request) as response:
response_body = response.read()
#!/usr/bin/env python3
import requests
response = requests.post("https://httpbin.org/post", files={'file': open('nasa_black_hole.png', 'rb')})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment