Skip to main content
 首页 » 编程设计

python如何拆分http请求行

2024年10月01日7tintown

这是我的请求行示例:

GET https://example.com/hello?ageSelections=0&category=6000&productPeriodType=3&resourcePlace=resourcePlace AND 5089=5089-- PStU&twoCategory=6200 HTTP/1.1 

我想拆分这个字符串并期待结果:

['GET', 'https://example.com/hello?ageSelections=0&category=6000&productPeriodType=3&resourcePlace=resourcePlace AND 5089=5089-- PStU&twoCategory=6200', 'HTTP/1.1'] 

谢谢:-)

请您参考如下方法:

使用 str.partition 和 str.rpartition 的两步方法(也可以通过 str.split 和 str .rsplit 每次都有一个 maxsplit=1 参数:

line = 'GET https://example.com/hello?ageSelections=0&category=6000&productPeriodType=3&resourcePlace=resourcePlace AND 5089=5089-- PStU&twoCategory=6200 HTTP/1.1' 
method, _, rest = line.partition(' ') 
url, _, protocol = rest.rpartition(' ') 
parsed_line = [method, url, protocol]