lunes, 15 de julio de 2024

Python response module

Exceptions and Warnings

ConnectTimeout:


import requests from requests.exceptions import ConnectTimeout try: response = requests.get('https://httpbin.org/delay/10', timeout=1) except ConnectTimeout: print("The request timed out while trying to connect to the server.")

########################

ConnectionError:

from requests.exceptions import ConnectionError
try: response = requests.get('1http://thisurldoesnotexist.com') except ConnectionError: print("A network problem occurred.")

#################

DependencyWarning:



import warnings from requests import DependencyWarning warnings.warn("This is a DependencyWarning", DependencyWarning)

################

FileModeWarning:


import warnings from requests import FileModeWarning warnings.warn("This is a FileModeWarning", FileModeWarning)

#####################

HTTPError:



from requests.exceptions import HTTPError try: response = requests.get('https://httpbin.org/status/404') response.raise_for_status() except HTTPError as http_err: print(f"HTTP error occurred: {http_err}")

######################################

JSONDecodeError:

from requests.exceptions import JSONDecodeError
try: response = requests.get('https://httpbin.org/get') data = response.json() except JSONDecodeError: print("Failed to decode JSON response.")

#####

NullHandler:

import logging from requests import NullHandler logger = logging.getLogger('example_logger') logger.addHandler(NullHandler()) logger.info('This will not be output anywhere')


############

ReadTimeout:

from requests.exceptions import ReadTimeout try: response = requests.get('https://httpbin.org/delay/3', timeout=1) except ReadTimeout: print("The server did not send any data in the allotted amount of time.")



No hay comentarios:

Publicar un comentario