首页 > 编程语言 >Python request请求 解析

Python request请求 解析

时间:2023-05-20 16:35:46浏览次数:52  
标签:HTTP http Python request header client DEBUG logging 解析

import logging
import http.client

httpclient_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()
>>> r = requests.get('http://httpbin.org/get?foo=bar&baz=python')
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org:80
DEBUG:http.client:send: b'GET /get?foo=bar&baz=python HTTP/1.1\r\nHost: httpbin.org\r\nUser-Agent: python-requests/2.22.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\n\r\n'
DEBUG:http.client:reply: 'HTTP/1.1 200 OK\r\n'
DEBUG:http.client:header: Date: Tue, 04 Feb 2020 13:36:53 GMT
DEBUG:http.client:header: Content-Type: application/json
DEBUG:http.client:header: Content-Length: 366
DEBUG:http.client:header: Connection: keep-alive
DEBUG:http.client:header: Server: gunicorn/19.9.0
DEBUG:http.client:header: Access-Control-Allow-Origin: *
DEBUG:http.client:header: Access-Control-Allow-Credentials: true
DEBUG:urllib3.connectionpool:http://httpbin.org:80 "GET /get?foo=bar&baz=python HTTP/1.1" 200 366

默认的request ,logging 会记录红色的请求:
比如:
2023-05-19 18:10:59,425 - DEBUG: Starting new HTTP connection (1): http://localhost:5000
2023-05-19 18:10:59,622 - DEBUG: http://localhost:5000 "POST /api/sale/s HTTP/1.1" 200 None


1. 在2023-05-19 18:10:59,425,程序启动了一个到http://localhost:5000的新HTTP连接
2. 在2023-05-19 18:10:59,622,也就是197ms后,通过此连接发送了一个POST /api/sale/s HTTP/1.1请求
3. 服务器返回的状态码是200,表示请求成功
4. 返回的响应内容长度为None,表明此日志没有显示响应内容长度信息


https://stackoverflow.com/questions/16337511/log-all-requests-from-the-python-requests-module

  

标签:HTTP,http,Python,request,header,client,DEBUG,logging,解析
From: https://www.cnblogs.com/cbugs/p/17417404.html

相关文章

  • python 检测屏幕指定区域 有变化即截图
    fromPILimportImageChops#$pipinstallpillowfrompyscreenshotimportgrab#$pipinstallpyscreenshotfromdatetimeimportdatetimeimporttime,sys,reim=grab(bbox=(160,180,1760,1080))a=0whileTrue:#http://effbot.org/zone/pil-comparing-imag......
  • 基于python实现-根据Excel表格指定的UniqueKey的顺序-到另一个参考表格中查找-补全与
    今天笔者在整理一份数据时,有这样一个需求,已知有多个ID是UniqueKey,每一个UniqueKey及与它相关的数据为一行,存放于Excel表格行中但他们相关的数据可能有误,而另一个表格Excel-02中的数据没有问题,但是UniqueKey顺序与第一个表格不一样现在主要是要修改第一个表格的数据,当然可以使用......
  • 使用Python实现MACD策略
    importpandasaspdimportnumpyasnpdefcalculate_macd(data,short_period=12,long_period=26,signal_period=9):"""计算MACD指标和信号线参数:data:包含价格数据的DataFrame,需包含'Close'列short_period:快线的计算周期,默认为12long_p......
  • Python selenium
    初始化webdriveropts=webdriver.chrome.options.Options()#无头模式opts.add_argument("--headless")opts.add_argument("--disable-gpu")#驱动地址driver_path=os.path.join(os.path.dirname(__file__),"./driver/chromedriver.exe"......
  • C#Xml文档的解析
    1<?xmlversion="1.0"encoding="utf-8"?>2<skills>3<skill>4<id>1</id>5<namelang="cn">a</name>6<damage>100</damage>7</skill>......
  • servJump extends HttpServlet //request.getRequestDispatcher //response.sendRed
    packagecom.test;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.HashMap;importjava.util.Map;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;impo......
  • 用python读取excel文件
    需要用到的包--pandasimportpandasaspd简单读取excel文件,要用到read_excel()df=pd.read_excel("D:\无名字的文件夹\实验材料.xlsx")dfOut[11]:姓名年龄喜好0张三15抖音1李四16快手2王五17抖音3小明18小红书4小花19小红书使用r......
  • python-docx - 1
    python-docx用于创建和更新Word文件的python库1.安装pip3installpython-docx-ihttps://mirrors.aliyun.com/pypi/simple2.创建与保存文件#导入DocumentfromdocximportDocument#创建一个新文档doc=Document()print(type(doc))#<class'docx.document.Doc......
  • 用python处理word文档的操作
    提取超链接fromdocximportDocument注意docx包不是python自带的包需要下载下一篇,我会给大家说相关的下载,可以点这里跳转到下一篇博客fromdocximportRTimportred=Document('D:\无名字的文件夹\python练习\材料.docx')forpind.paragraphs:rels=d.part.relsforrel......
  • Python潮流周刊#1:如何系统地自学Python?
    这里记录每周值得分享的Python及通用技术内容,部分内容为英文,已在小标题注明。(本期标题取自其中一则分享,不代表全部内容都是该主题,特此声明。)文章&教程1、编程语言的错误处理模式文章讨论了编程中处理错误的四种常见方法:返回错误代码(C、Go)、异常(Java、C#、Python、C++)、回调函数(Jav......