这是我的请求行示例:
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]
关于python 如何分割http请求线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47706721/