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 23 Python
django manage.py扩展自定义命令方法
May 27 Python
NumPy 数学函数及代数运算的实现代码
Jul 18 Python
scrapy-redis源码分析之发送POST请求详解
May 15 Python
python向字符串中添加元素的实例方法
Jun 28 Python
Django框架模型简单介绍与使用分析
Jul 18 Python
Python如何使用字符打印照片
Jan 03 Python
Python实现CNN的多通道输入实例
Jan 17 Python
关于python3.9安装wordcloud出错的问题及解决办法
Nov 02 Python
python 利用jieba.analyse进行 关键词提取
Dec 17 Python
Python的logging模块基本用法
Dec 24 Python
Python实现京东抢秒杀功能
Jan 25 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翻页类
2009/06/01 PHP
PHP 时间日期操作实战
2011/08/26 PHP
php中关于socket的系列函数总结
2015/05/18 PHP
又十个超级有用的PHP代码片段
2015/09/24 PHP
Composer设置忽略版本匹配的方法
2016/04/27 PHP
jquery获取被勾选的checked(选中)的那一行的3列和4列的值
2013/07/04 Javascript
js获取html文件的思路及示例
2013/09/17 Javascript
JS实现字符串转日期并比较大小实例分析
2015/12/09 Javascript
KnockoutJS 3.X API 第四章之数据控制流with绑定
2016/10/10 Javascript
vue基础之使用get、post、jsonp实现交互功能示例
2019/03/12 Javascript
layui实现数据表格自定义数据项
2019/10/26 Javascript
python进阶教程之动态类型详解
2014/08/30 Python
python optparse模块使用实例
2015/04/09 Python
python实现rsa加密实例详解
2017/07/19 Python
Python实现的多线程同步与互斥锁功能示例
2017/11/30 Python
python求解数组中两个字符串的最小距离
2018/09/27 Python
Python类的继承用法示例
2019/01/31 Python
python binascii 进制转换实例
2019/06/12 Python
Python实现性能自动化测试竟然如此简单
2019/07/30 Python
python导入库的具体方法
2020/06/18 Python
Python基于pillow库实现生成图片水印
2020/09/14 Python
css3制作彩色边线3d立体按钮的示例(css3按钮)
2014/05/06 HTML / CSS
HTML5教程之html 5 本地数据库(Web Sql Database)
2014/04/03 HTML / CSS
英国领先的男士美容护发用品公司:Mankind
2016/08/31 全球购物
英国最受欢迎的价格比较网站之一:MoneySuperMarket
2018/12/19 全球购物
信息管理员岗位职责
2013/12/01 职场文书
职业技术学校毕业生推荐信
2013/12/03 职场文书
经理秘书找工作求职信
2013/12/19 职场文书
大众服装店创业计划书范文
2014/01/01 职场文书
护士进修自我鉴定
2014/02/07 职场文书
贺卡寄语大全
2014/04/11 职场文书
和解协议书
2014/04/16 职场文书
新颖的化妆品活动方案
2014/08/21 职场文书
小学美术教学反思
2016/02/17 职场文书
基于Python的EasyGUI学习实践
2021/05/07 Python
浅析MySQL如何实现事务隔离
2021/06/26 MySQL