scientific_comp_projects/CODE/[python]XML_JSON/reqerrs_intro.py
2021-10-29 15:16:40 +02:00

46 lines
1.2 KiB
Python

# using the requests library to catch errors
'''
Making a simple request
reponse = requests.get(url)
params | Key-value pairs that will be sent in the query string.
header | dictionary of header vaues to send along with the request
auth | Authentication tuple to enable different forms of authentication
timeout | Value in seconds to wait for a server to respond
'''
import requests
from requests import HTTPError,Timeout
def main():
try :
# url = 'http://httpbin.org/status/404'
url = 'http://httpbin.org/delay/5'
result = requests.get(url, timeout=2)
result.raise_for_status()
printResults(result)
except HTTPError as err :
print('Error : {0}'.format(err))
except Timeout as err :
print('Request timed out : {0}'.format(err))
def printResults(resData):
print('Result code : {0}'.format(resData.status_code))
print('\n')
print('Headers : ------------------------------')
print(resData.headers)
print('\n')
print('Returned data : ------------------------')
print(resData.text)
if __name__=="__main__":
main()