Python Http请求json解析库用法解析


Posted in Python onNovember 28, 2020

httpparser介绍

:1.解析字节类型的http与https请求数据

:2.支持已k-v形式修改请求数据

:3.支持重新编码请求数据

源码

import json
__author = "-ling"

def parser(request_data):
  # 获取请求的三个段:
  # 1.请求方法 URI协议 版本
  # 2.请求头(Request Header)
  # 3.请求正文
  index0 = request_data.find(b"\r\n\r\n")
  request_predata = request_data[0:index0]
  index1 = request_predata.find(b"\r\n")

  # 请求方法 URI协议 版本
  request_first_data = request_predata[0:index1].decode("utf-8")
  request_first = {}
  count = 0
  list = ["method", 'url', 'version']
  for line in request_first_data.split(" "):
    if line != "":
      request_first[list[count]] = line
      count += 1
  # print("解析请求方法 URI协议 版本:",request_first)

  # 请求头(Request Header)
  request_header_data = request_predata[index1:].decode("utf-8")
  request_headers = {}
  for line in request_header_data.split("\r\n"):
    if line != "":
      line = line.replace(" ","")
      restemp = line.split(":")
      if restemp[0] == "Host" and len(restemp) == 3:
        restemp[1] = restemp[1] + ":" +restemp[2]
      request_headers[restemp[0]] = restemp[1]
  # print("请求头(Request Header):",request_headers)

  # 请求正文
  request_nextdata = request_data[index0:].decode("utf-8")
  request_content_temp = request_nextdata.replace("\r\n", "")
  request_content = None
  if request_content_temp != "":

 try:
    
request_content = json.loads(request_content_temp)


 except:



 request_content = {'content':request_content_temp}

    # print("请求正文:",request_content)
  else:
    pass
    # print("无请求正文!")
  return request_first,request_headers,request_content,request_nextdata

def update_first_data(request_first_data,field,data):
  request_first_data[field] = data


def update_request_headers(request_headers,field,data):
  request_headers[field] = data


def update_request_content(request_content,field,data):
  request_content[field] = data


def encode(request_first_data,request_headers,request_content):
  request_data = b""
  list = ["method", 'url', 'version']
  for key in list:
    request_data += (request_first_data[key] + " ").encode("utf-8")
  request_data += "\r\n".encode("utf-8")
  for key in request_headers.keys():
    request_data += (key + ":" + request_headers[key]).encode("utf-8")
    request_data += "\r\n".encode("utf-8")
  request_data += "\r\n".encode("utf-8")
  if request_content != None:
      request_data += json.dumps(request_content).encode("utf-8")
  # print("重新编码以后的数据:",request_data.decode("utf-8"))
  return request_data

如何使用

1.解析请求数据

request_first,request_headers,request_content,request_nextdata = httpparser.parser(request_data)

2.修改或者增加各个部分的字段使用

  • update_first_data :修改第一行字段数据
  • update_request_headers :修改请求头或者增加请求头字段
  • update_request_content :修改请求内容字段或者增加请求内容

3.再编码三个部分的数据

encode(request_first_data,request_headers,request_content)

示例(http返回数据如下):

b'HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 13\r\nServer: Werkzeug/1.0.1 Python/3.7.7\r\nDate: Thu, 15 Oct 2020 02:58:54 GMT\r\n\r\n<h1>foo!</h1>'

解析出来的数据:

注意:(parser传入字节类型数据)

解析数据: {'method': 'HTTP/1.0', 'url': '200', 'version': '

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
举例讲解Python中装饰器的用法
Apr 27 Python
Python对文件操作知识汇总
May 15 Python
利用Python读取文件的四种不同方法比对
May 18 Python
利用python批量给云主机配置安全组的方法教程
Jun 21 Python
Python编程实现线性回归和批量梯度下降法代码实例
Jan 04 Python
python学习笔记--将python源文件打包成exe文件(pyinstaller)
May 26 Python
Python基于pandas实现json格式转换成dataframe的方法
Jun 22 Python
Python中对数组集进行按行打乱shuffle的方法
Nov 08 Python
python实现感知机线性分类模型示例代码
Jun 02 Python
python字典的setdefault的巧妙用法
Aug 07 Python
Python 限定函数参数的类型及默认值方式
Dec 24 Python
Python图像处理库PIL的ImageDraw模块介绍详解
Feb 26 Python
基于Django集成CAS实现流程详解
Nov 28 #Python
Django haystack实现全文搜索代码示例
Nov 28 #Python
windows下python 3.9 Numpy scipy和matlabplot的安装教程详解
Nov 28 #Python
关于Python 解决Python3.9 pandas.read_excel(‘xxx.xlsx‘)报错的问题
Nov 28 #Python
Python self用法详解
Nov 28 #Python
Python3.9最新版下载与安装图文教程详解(Windows系统为例)
Nov 28 #Python
python安装sklearn模块的方法详解
Nov 28 #Python
You might like
php高性能日志系统 seaslog 的安装与使用方法分析
2020/02/29 PHP
基于jquery+thickbox仿校内登录注册框
2010/06/07 Javascript
基于JQuery制作的产品广告效果
2010/12/08 Javascript
通过jquery还原含有rowspan、colspan的table的实现方法
2012/02/10 Javascript
简单谈谈javascript中的变量、作用域和内存问题
2015/08/30 Javascript
jQuery实现简洁的导航菜单效果
2015/11/23 Javascript
理解javascript定时器中的setTimeout与setInterval
2016/02/23 Javascript
AngularJS双向数据绑定原理之$watch、$apply和$digest的应用
2018/01/30 Javascript
详解vue-cli中使用rem,vue自适应
2019/05/06 Javascript
Vue对象赋值视图不更新问题及解决方法
2019/06/03 Javascript
Vue项目中如何使用Axios封装http请求详解
2019/10/23 Javascript
react 原生实现头像滚动播放的示例
2020/04/21 Javascript
解决vue中axios设置超时(超过5分钟)没反应的问题
2020/09/04 Javascript
使用Vant完成Dialog弹框案例
2020/11/11 Javascript
Python显示进度条的方法
2014/09/20 Python
简单的Python2.7编程初学经验总结
2015/04/01 Python
浅析Python中的多条件排序实现
2016/06/07 Python
Django视图之ORM数据库查询操作API的实例
2017/10/27 Python
Python使用numpy实现BP神经网络
2018/03/10 Python
Python中pandas dataframe删除一行或一列:drop函数详解
2018/07/03 Python
Python中使用Counter进行字典创建以及key数量统计的方法
2018/07/06 Python
python读出当前时间精度到秒的代码
2019/07/05 Python
tensorflow estimator 使用hook实现finetune方式
2020/01/21 Python
python实现读取类别频数数据画水平条形图案例
2020/04/24 Python
CSS3中border-radius属性设定圆角的使用技巧
2016/05/10 HTML / CSS
基于第一个PhoneGap(cordova)的应用详解
2013/05/03 HTML / CSS
美国男装连锁零售商:Men’s Wearhouse
2016/10/14 全球购物
导购员的岗位职责
2014/02/08 职场文书
人事任命书范文
2014/06/04 职场文书
个人优缺点总结
2015/02/28 职场文书
学生会招新宣传语
2015/07/13 职场文书
2015年汽车销售员工作总结
2015/07/24 职场文书
慰问信(范文3篇)
2019/10/23 职场文书
MySQL数据库优化之通过索引解决SQL性能问题
2022/04/10 MySQL
tomcat默认最大连接数及相关调整方法
2022/05/06 Servers
Win11怎么添加用户?Win11添加用户账户的方法
2022/07/15 数码科技