Skip to content

Instantly share code, notes, and snippets.

@progala
Created May 25, 2022 12:09
Show Gist options
  • Save progala/e387a640c2c6437567b411fb087a09f8 to your computer and use it in GitHub Desktop.
Save progala/e387a640c2c6437567b411fb087a09f8 to your computer and use it in GitHub Desktop.
Enable detailed logging for Python `requests`
# 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