HTTP协议字符串的切割解析基础

一、引言

在我们学习了http协议的结构后,我们肯定迫不及待地想要进行练习和巩固。

我们可以使用一个Request类来负责封装http协议发送的字符串信息。在解析的时候我们可以巩固常用的基本属性,如host、path、port、method、header等以及对于字符串和类的使用。

二、意义

为什么需要做这个呢?服务器不是已经帮我们封装好了吗?

说的没错........服务器已经都帮我们清晰的封装好了。

不过通过自己再次对原装的字符串进行切割,可以帮助我们更好的复习http协议的结构形式以及对于字符串语法的使用。

所以本篇博客比较适合于刚学习完http协议的基本结构且还不是那么熟悉的新手。

三、实例演练

get请求的示例

python 复制代码
GET /api/v1/resource/1234?param1=value1&param2=value2 HTTP/1.1 

Host: www.example.com
User-Agent: curl/7.64.1 
Accept: */*
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate 
Connection: keep-alive

以上代码可以很清晰的看出http协议发送的字符串结构。下面我们就来进行代码解析。

python 复制代码
class Request:
    def __init__(self,method,path,protocol,headers):
        self.method=method
        self.path=path
        self.protocol=protocol
        self.headers=headers
        
        
    @staticmethod
    def parse_request(data):
        parts=data.strip().split('\n\n')
        request_line,headers=parts[0],parts[1]
        method,path,protocol=request_line.split(' ')
        header_lines=headers.split('\n')
        
        headers={}
        for header in header_lines:
            key,value=header.split(':')
            headers[key]=value
            
        return Request(method,path,protocol,headers)
        
request=Request.parse_request(data)

print(request.method)
print(request.path)
print(request.protocol)
print(request.headers)

运行结果如下

解释一下方法: 总体来说就是先根据两个换行符\n来分为两个部分,一个是request_line,一个是headers,再分别切割。 request_line根据字符之间的空格' '来划分,再一个一个对应方法、路径和版本。 headers同理。最后返回Request,再输出出来。

post请求的示例

python 复制代码
data = """POST /api/v1/resource HTTP/1.1  
Host: www.example.com  
User-Agent: curl/7.64.1  
Accept: */*  
Accept-Language: en-US,en;q=0.5  
Accept-Encoding: gzip, deflate  
Connection: keep-alive  
Content-Type: application/json  
Content-Length: 81  
  
{  
"key1": "value1",  
"key2": "value2",  
"key3": {  
"subKey": "subValue"  
}  
}  
"""

解析代码

python 复制代码
class Request:  
def __init__(self, method, path, version, headers, body):  
self.method = method  
self.path = path  
self.version = version  
self.headers = headers  
self.body = body  
  
  
def parse_request(data):  
lines = data.split('\n')  
method, path, version = lines[0].split(' ')  
headers = {}  
index = lines.index('')  
for line in lines[1:index]:  
key, value = line.split(':')  
headers[key] = value  
body = '\n'.join(lines[index + 1:])  
  
request = Request(method, path, version, headers, body)  
return request  
  
  
request = parse_request(data)  
print(request.method)  
print(request.path)  
print(request.version)  
print(request.headers)  
print(request.body)

运行结果如下

说一下思路:其实本质上都差不多,具体的分割方法得看具体的data结构,比如这个例子就不像get方法的那个一样用\n\n来分块,它没有连续的换行,只能\n后一行一行解析,lines[0]就是第一行。以此类推,后面使用index索引来查找想要的行数,还是很好用的。

四、总结

通过一系列的切割练习,我们可以更好的巩固http协议的结构,以及对于python基本语法的强化练习。

当然了这只是示例,实际在运用的时候会有更难更复杂的情况,一步一步来吧,那都是后话了。(笑)

相关推荐
孤狼warrior1 小时前
灰色预测模型
人工智能·python·算法·数学建模
神仙别闹1 小时前
基于Python实现LSTM对股票走势的预测
开发语言·python·lstm
机器学习之心1 小时前
小波增强型KAN网络 + SHAP可解释性分析(Pytorch实现)
人工智能·pytorch·python·kan网络
JavaEdge在掘金2 小时前
MySQL 8.0 的隐藏索引:索引管理的利器,还是性能陷阱?
python
站大爷IP2 小时前
Python办公自动化实战:手把手教你打造智能邮件发送工具
python
chao_7892 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
zdw2 小时前
fit parse解析佳明.fit 运动数据,模仿zwift数据展示
python
剑桥折刀s3 小时前
Python打卡:Day46
python
巴里巴气3 小时前
Python爬虫图片验证码和滑块验证码识别总结
爬虫·python
sword devil9004 小时前
PYQT实战:智能家居中控
python·智能家居·pyqt