Python/requests
< Python
Jump to navigation
Jump to search
Standard Usage
Simulate conditions.
import requests
from requests.exceptions import RequestException
def send_request(url, params={}):
try:
resp = requests.get(url, params=params, allow_redirects=False, timeout=1.5)
if resp.status_code == 200:
print('%-40s | HTTP OK' % url)
else:
print('%-40s | HTTP Status: %s' % (url, resp.status_code))
except RequestException as ex:
print('%-40s | RequestException: %s' % (url, type(ex).__name__))
def main(quickly=True):
mini_list = [200, 302, 404, 500]
full_list = [
100, 101, 102, 103,
200, 201, 202, 203, 204, 205, 206,
300, 301, 302, 303, 304, 305, 306, 307, 308,
400, 401, 402, 403, 404, 405, 406, 407, 408, 409,
410, 411, 412, 413, 414, 415, 416, 417, 418,
421, 422, 423, 425, 426, 428, 429,
431, 451,
500, 501, 502, 503, 504, 505, 506,
511, 520, 522, 524
]
code_list = mini_list if quickly else full_list
for code in code_list:
url = 'https://httpstat.us/%s' % code
send_request(url)
params = { 'sleep': 1600 }
url = 'https://httpstat.us/200'
send_request(url, params)
url = 'httpx://httpstat.us/200'
send_request(url)
if __name__ == '__main__':
main()
Output
$ python3 requests_feed.py
https://httpstat.us/200 | HTTP OK
https://httpstat.us/302 | HTTP Status: 302
https://httpstat.us/404 | HTTP Status: 404
https://httpstat.us/500 | HTTP Status: 500
https://httpstat.us/200?sleep=2000 | RequestException: ReadTimeout
httpx://httpstat.us/200 | RequestException: InvalidSchema
Hierarchy of Exceptions
See: https://requests.kennethreitz.org/en/master/_modules/requests/exceptions/#RequestException