Skip to content

Instantly share code, notes, and snippets.

@nicc777
Forked from hartsock/debug_http.py
Created September 25, 2023 06:54
Show Gist options
  • Save nicc777/5bb8a56400463450403f00d7d201c9e2 to your computer and use it in GitHub Desktop.
Save nicc777/5bb8a56400463450403f00d7d201c9e2 to your computer and use it in GitHub Desktop.
A snippet of code that shows how to turn on debugging for the Python HTTP client library
import requests
import logging
# These two lines enable debugging at httplib 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:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment