python-zeep
python-zeep copied to clipboard
Access to HTTP request?
Is it possible to directly access the request (python requests) object for low level HTTP handling?
I am writing a logger plugin and wanted to also log raw HTTP-related things, such as the method, request headers, request body, response headers, response body.
You can generally turn on logging by the requests module like this
import logging
from http.client import HTTPConnection
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
debug_requests_on()
Sadly I don't have the link from where I got it, but it's proven very useful
Another way you can do this is by creating request/response hooks for a session and using that session as transport
from zeep.client import Client
from zeep.transports import Transport
from requests import Session, Response
def log_traffic(r: Response):
print(r.headers, r.request.body, "etc...")
with Session() as s:
s.hooks["response"].append(lambda r, *args, **kwargs: log_traffic(r))
transport = Transport(session=s)
c = Client(transport=transport)
response = c.service.myoperation()