gpt4 book ai didi

python - 为什么从浏览器发送的表单和 SimpleHttpServer 捕获的请求之间存在差异

转载 作者:可可西里 更新时间:2023-11-01 16:20:30 25 4
gpt4 key购买 nike

我有带有 `do_POST' 功能的 SimleHttpServer

def do_POST(self):

print self.path

form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
for key in form.keys():
print key

现在的问题是,如果我用浏览器发送请求(像这样的 HTML 格式)

<form method="post" action="/">
<input type="text" name="a" value="1"><br>
<input type="submit" value="Send">
</form>

那么值是:

self.path = /
form.keys = [a]

如果我用 python Requests 发送相同的请求

parameters = {
"a": 2,
}

headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)",
"Content-Type": "application/x-www-form-urlencoded",
"Referer": "http://127.0.0.1:8001/",
"Host": "127.0.0.1:8001",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
}

r = requests.post("http://127.0.0.1:8001", params=parameters, headers=headers)

服务器上的值是

self.path = /?a=2
form.keys = []

问题出在哪里?有什么不同吗?我从浏览器复制了标题,所以一切都应该是一样的。

在此先感谢您提供任何帮助或线索。

解决方法:

如果遇到此问题,可以在 do_POST 中使用以下解决方法(需要导入 cgi、urlparse):

    if "?" in self.path:
qs = self.path.split("?")[1]
query_dict = dict(urlparse.parse_qsl(qs))
else:
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
query_dict = {key: form.getvalue(key) for key in form.keys()}

但这不是问题的答案。

最佳答案

如果您想以 POST 方式发送数据(即不是 GET 查询字符串参数),您应该将其作为 data 参数传递,而不是 params:

Typically, you want to send some form-encoded data — much like an HTML form. To do this, simply pass a dictionary to the data argument. Your dictionary of data will automatically be form-encoded when the request is made

(来源:More complicated POST requests)

params -- (optional) Dictionary or bytes to be sent in the query string for the Request.

data -- (optional) Dictionary or list of tuples [(key, value)] (will be form-encoded), bytes, or file-like object to send in the body of the Request.

(来源:Main Interface,强调我的)

关于python - 为什么从浏览器发送的表单和 SimpleHttpServer 捕获的请求之间存在差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43208645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com