Last active
February 3, 2022 18:31
-
-
Save tonejito/2b2bcb901c1cc0ce4a4b7ce44e578e71 to your computer and use it in GitHub Desktop.
Test for the Python requests module
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
#!/usr/bin/env python3 | |
# Test for the Python requests module | |
# https://docs.python-requests.org/en/latest/user/quickstart/ | |
# https://docs.python-requests.org/en/latest/user/advanced/ | |
# https://docs.python-requests.org/en/latest/api/ | |
import requests | |
# This URL will return the status code passed in the parameter | |
URL = "https://httpstat.us/403" | |
# You can check if the status code is within the "good" range | |
# Ignore SSL verification (the same as curl -k) | |
# Also, we are allowing redirects | |
if requests.get(URL, verify=False, allow_redirects=True).status_code in range(100, 399): | |
print("The HTTP request was successful") | |
else: | |
print("The HTTP request was not successful") | |
print("The status code was:", response.status_code) |
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
#!/usr/bin/env python3 | |
# Test for the Python requests module | |
# https://docs.python-requests.org/en/latest/user/quickstart/ | |
# https://docs.python-requests.org/en/latest/user/advanced/ | |
# https://docs.python-requests.org/en/latest/api/ | |
import logging | |
import requests | |
import urllib3 | |
# Ignore SSL warnings | |
urllib3.disable_warnings() | |
logging.captureWarnings(True) | |
# This URL responds OK and will not raise an exception | |
# URL="https://example.com/" | |
# This URL requires authentication and will raise an exception | |
# since the status code wasn't in the 1xx, 2xx, or 3xx family | |
URL = "https://httpstat.us/403" | |
# Ignore SSL verification (the same as curl -k) | |
# Also, we are allowing redirects | |
response = requests.get(URL, verify=False, allow_redirects=True) | |
# Print misc info about the HTTP request | |
print("Request headers") | |
for header in response.request.headers: | |
print("> {}: {}".format(header, response.request.headers[header])) | |
# Print misc info about the HTTP response | |
print() | |
print("Response information") | |
print("URL:", response.url) | |
print("Status code:", response.status_code) | |
for header in response.headers: | |
print("< {}: {}".format(header, response.headers[header])) | |
print("Text:", response.text) | |
print() | |
# You can check if the status code is within the good range | |
if response.status_code in range(100, 399): | |
print("The HTTP request was successful") | |
else: | |
print("The HTTP request was not successful") | |
print("The status code was:", response.status_code) | |
# This will raise an exception if the status code is not successful | |
try: | |
response.raise_for_status() | |
except requests.exceptions.HTTPError as e: | |
print("There was an HTTP error:", e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment