捕获POST方法的查询字符串并在python中转换为字典?
capture the query strings of POST method and turn into dictionary in python?
如何get/capture将POST方法的查询字符串转化为python中的字典?
这是 request.txt 文件的内容。仅供参考,查询字符串参数可能因请求文件而异。
POST /login HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/62.0
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost/login
Cookie: __cfduid=dcd3e07532; XSRF-TOKEN=eyJpdiIyYzc0In0%3D; key_session=eyJpdiI6IFmZCJ9
_token=VCXB1YcU3ti&email=testmail%40gmail.com&password=pass12345
这是我的代码:
from urllib.parse import urlparse, parse_qs
#read last line
with open("request.txt", "r") as file:
first_line = file.readline()
for last_line in file:
pass
是否有使用拆分或其他方式获取查询字符串的最佳方法?不参考 request.txt 文件中的最后一行?非常感谢
from urllib import parse
with open("data.txt") as f:
lines = f.readlines()
goal = lines[lines.index("\n")+1]
print(dict(parse.parse_qsl(parse.urlsplit(goal).path)))
输出:
{'_token': 'VCXB1YcU3ti', 'email': 'testmail@gmail.com', 'password': 'pass12345'}
如何get/capture将POST方法的查询字符串转化为python中的字典? 这是 request.txt 文件的内容。仅供参考,查询字符串参数可能因请求文件而异。
POST /login HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/62.0
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost/login
Cookie: __cfduid=dcd3e07532; XSRF-TOKEN=eyJpdiIyYzc0In0%3D; key_session=eyJpdiI6IFmZCJ9
_token=VCXB1YcU3ti&email=testmail%40gmail.com&password=pass12345
这是我的代码:
from urllib.parse import urlparse, parse_qs
#read last line
with open("request.txt", "r") as file:
first_line = file.readline()
for last_line in file:
pass
是否有使用拆分或其他方式获取查询字符串的最佳方法?不参考 request.txt 文件中的最后一行?非常感谢
from urllib import parse
with open("data.txt") as f:
lines = f.readlines()
goal = lines[lines.index("\n")+1]
print(dict(parse.parse_qsl(parse.urlsplit(goal).path)))
输出:
{'_token': 'VCXB1YcU3ti', 'email': 'testmail@gmail.com', 'password': 'pass12345'}