栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

记录来自python-requests模块的所有请求

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

记录来自python-requests模块的所有请求

基础

urllib3
库记录与
logging
模块(而不是
POST
主体)的所有新连接和URL
。对于
GET
请求,这应该足够了:

import logginglogging.basicConfig(level=logging.DEBUG)

这为您提供了最详细的日志记录选项;有关如何配置日志记录级别和目标的更多详细信息,请参见日志记录操作指南。

简短的演示:

>>> import requests>>> import logging>>> logging.basicConfig(level=logging.DEBUG)>>> r = requests.get('http://httpbin.org/get?foo=bar&baz=python')DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org:80DEBUG:urllib3.connectionpool:http://httpbin.org:80 "GET /get?foo=bar&baz=python HTTP/1.1" 200 366

根据urllib3的确切版本,将记录以下消息:

  • INFO
    :重定向
  • WARN
    :连接池已满(如果发生这种情况,通常会增加连接池的大小)
  • WARN
    :无法解析标头(格式无效的响应标头)
  • WARN
    :重试连接
  • WARN
    :证书与预期的主机名不匹配
  • WARN
    :处理分块响应时,接收到的内容长度和传输编码都包含响应
  • DEBUG
    :新连接(HTTP或HTTPS)
  • DEBUG
    :连接断开
  • DEBUG
    :连接详细信息:方法,路径,HTTP版本,状态代码和响应长度
  • DEBUG
    :重试计数增量

这不包括标题或正文。

urllib3
使用
http.client.HTTPConnection
该类完成任务,但是该类不支持日志记录,通常只能将其配置为
打印 到stdout。但是,您可以绑定该模块,以将所有调试信息发送到日志记录,而无需
print
在该模块中引入其他名称:

import loggingimport http.clienthttpclient_logger = logging.getLogger("http.client")def httpclient_logging_patch(level=logging.DEBUG):    """Enable HTTPConnection debug logging to the logging framework"""    def httpclient_log(*args):        httpclient_logger.log(level, " ".join(args))    # mask the print() built-in in the http.client module to use    # logging instead    http.client.print = httpclient_log    # enable debugging    http.client.HTTPConnection.debuglevel = 1

调用

httpclient_logging_patch()
导致
http.client
连接将所有调试信息输出到标准记录器,因此被以下对象拾取
logging.basicConfig()

>>> httpclient_logging_patch()>>> r = requests.get('http://httpbin.org/get?foo=bar&baz=python')DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org:80DEBUG:http.client:send: b'GET /get?foo=bar&baz=python HTTP/1.1rnHost: httpbin.orgrnUser-Agent: python-requests/2.22.0rnAccept-Encoding: gzip, deflaternAccept: */*rnConnection: keep-alivernrn'DEBUG:http.client:reply: 'HTTP/1.1 200 OKrn'DEBUG:http.client:header: Date: Tue, 04 Feb 2020 13:36:53 GMTDEBUG:http.client:header: Content-Type: application/jsonDEBUG:http.client:header: Content-Length: 366DEBUG:http.client:header: Connection: keep-aliveDEBUG:http.client:header: Server: gunicorn/19.9.0DEBUG:http.client:header: Access-Control-Allow-Origin: *DEBUG:http.client:header: Access-Control-Allow-Credentials: trueDEBUG:urllib3.connectionpool:http://httpbin.org:80 "GET /get?foo=bar&baz=python HTTP/1.1" 200 366


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516999.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号