Sie müssen das Debugging unter httplib
(requests
→ urllib3
→ httplib
) aktivieren.
Hier einige Funktionen sowohl Knebel (..._on()
und ..._off()
) oder haben sie vorübergehend auf:
import logging
import contextlib
try:
from http.client import HTTPConnection # py3
except ImportError:
from httplib import HTTPConnection # py2
def debug_requests_on():
'''Switches on logging of the requests module.'''
HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def debug_requests_off():
'''Switches off logging of the requests module, might be some side-effects'''
HTTPConnection.debuglevel = 0
root_logger = logging.getLogger()
root_logger.setLevel(logging.WARNING)
root_logger.handlers = []
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.WARNING)
requests_log.propagate = False
@contextlib.contextmanager
def debug_requests():
'''Use with 'with'!'''
debug_requests_on()
yield
debug_requests_off()
Demo Verwendung:
>>> requests.get('http://httpbin.org/')
<Response [200]>
>>> debug_requests_on()
>>> requests.get('http://httpbin.org/')
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org
DEBUG:requests.packages.urllib3.connectionpool:"GET/HTTP/1.1" 200 12150
send: 'GET/HTTP/1.1\r\nHost: httpbin.org\r\nConnection: keep-alive\r\nAccept-
Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.11.1\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Server: nginx
...
<Response [200]>
>>> debug_requests_off()
>>> requests.get('http://httpbin.org/')
<Response [200]>
>>> with debug_requests():
... requests.get('http://httpbin.org/')
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org
...
<Response [200]>
Sie das REQUEST, einschließlich HEADERS und Daten, und RESPONSE mit HEADERS aber ohne DATA. Das einzige, was fehlt, ist der response.body, der nicht eingeloggt ist.
Source
Die Antwort von @yohann zeigt, wie noch mehr Logging-Ausgabe erhalten, einschließlich der Header Sie senden. Es sollte die akzeptierte Antwort sein, nicht die von Martijn, die nicht die Header zeigt, die Sie über wireshark erhalten und stattdessen eine Anfrage manuell angepasst haben. – nealmcb