一种简单的方法:启用登录最新版本的请求(1.x及更高版本)。
请求用途http.client和logging模块配置到控制日志记录级别,如所描述这里。
示范
摘自链接文档的代码:
import requestsimport 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_clientexcept importError: # Python 2 import httplib as http_clienthttp_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 = Truerequests.get('https://httpbin.org/headers')
示例输出
$ python requests-logging.py INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): httpbin.orgsend: 'GET /headers HTTP/1.1rnHost: httpbin.orgrnAccept-Encoding: gzip, deflate, compressrnAccept: */*rnUser-Agent: python-requests/1.2.0 CPython/2.7.3 Linux/3.2.0-48-genericrnrn'reply: 'HTTP/1.1 200 OKrn'header: Content-Type: application/jsonheader: Date: Sat, 29 Jun 2013 11:19:34 GMTheader: Server: gunicorn/0.17.4header: Content-Length: 226header: Connection: keep-aliveDEBUG:requests.packages.urllib3.connectionpool:"GET /headers HTTP/1.1" 200 226
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)