Created
May 25, 2022 12:09
-
-
Save progala/e387a640c2c6437567b411fb087a09f8 to your computer and use it in GitHub Desktop.
Enable detailed logging for Python `requests`
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
# Taken from: https://requests.readthedocs.io/en/latest/api/#main-interface | |
import requests | |
import logging | |
# Enabling debugging at http.client level (requests->urllib3->http.client) | |
# you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA. | |
# the only thing missing will be the response.body which is not logged. | |
try: # for Python 3 | |
from http.client import HTTPConnection | |
except ImportError: | |
from httplib import HTTPConnection | |
HTTPConnection.debuglevel = 1 | |
logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests | |
logging.getLogger().setLevel(logging.DEBUG) | |
requests_log = logging.getLogger("urllib3") | |
requests_log.setLevel(logging.DEBUG) | |
requests_log.propagate = True | |
requests.get('https://httpbin.org/headers') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment