Python Http請(qǐng)求json解析庫(kù)用法解析
httpparser介紹
:1.解析字節(jié)類型的http與https請(qǐng)求數(shù)據(jù)
:2.支持已k-v形式修改請(qǐng)求數(shù)據(jù)
:3.支持重新編碼請(qǐng)求數(shù)據(jù)
源碼
import json
__author = "-ling"
def parser(request_data):
# 獲取請(qǐng)求的三個(gè)段:
# 1.請(qǐng)求方法 URI協(xié)議 版本
# 2.請(qǐng)求頭(Request Header)
# 3.請(qǐng)求正文
index0 = request_data.find(b"\r\n\r\n")
request_predata = request_data[0:index0]
index1 = request_predata.find(b"\r\n")
# 請(qǐng)求方法 URI協(xié)議 版本
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("解析請(qǐng)求方法 URI協(xié)議 版本:",request_first)
# 請(qǐng)求頭(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("請(qǐng)求頭(Request Header):",request_headers)
# 請(qǐng)求正文
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("請(qǐng)求正文:",request_content)
else:
pass
# print("無(wú)請(qǐng)求正文!")
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("重新編碼以后的數(shù)據(jù):",request_data.decode("utf-8"))
return request_data
如何使用
1.解析請(qǐng)求數(shù)據(jù)
request_first,request_headers,request_content,request_nextdata = httpparser.parser(request_data)
2.修改或者增加各個(gè)部分的字段使用
- update_first_data :修改第一行字段數(shù)據(jù)
- update_request_headers :修改請(qǐng)求頭或者增加請(qǐng)求頭字段
- update_request_content :修改請(qǐng)求內(nèi)容字段或者增加請(qǐng)求內(nèi)容
3.再編碼三個(gè)部分的數(shù)據(jù)
encode(request_first_data,request_headers,request_content)
示例(http返回?cái)?shù)據(jù)如下):
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>'
解析出來(lái)的數(shù)據(jù):
注意:(parser傳入字節(jié)類型數(shù)據(jù))
解析數(shù)據(jù): {'method': 'HTTP/1.0', 'url': '200', 'version': '
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python查看自己安裝的所有庫(kù)并導(dǎo)出的命令
這篇文章主要介紹了python查看自己安裝的所有庫(kù)并導(dǎo)出,主要包括查看安裝的庫(kù)通過(guò)命令查詢,導(dǎo)出庫(kù)安裝文件執(zhí)行命令,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
python如何實(shí)現(xiàn)數(shù)組反轉(zhuǎn)
這篇文章主要介紹了python如何實(shí)現(xiàn)數(shù)組反轉(zhuǎn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Python使用lambda拋出異常實(shí)現(xiàn)方法解析
這篇文章主要介紹了Python使用lambda拋出異常實(shí)現(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
python?字符串索引取值的實(shí)現(xiàn)示例
本文主要介紹了python?字符串索引取值的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Python利用PyPDF2庫(kù)獲取PDF文件總頁(yè)碼實(shí)例
這篇文章主要介紹了Python利用PyPDF2庫(kù)獲取PDF文件總頁(yè)碼實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
移除Selenium中window.navigator.webdriver值
這篇文章主要為大家介紹了如何正確的移除Selenium中window.navigator.webdriver的值方法步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
在python中實(shí)現(xiàn)對(duì)list求和及求積
今天小編就為大家分享一篇在python中實(shí)現(xiàn)對(duì)list求和及求積,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11

