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

Why is difference between form sent from browser and Requests when catched by SimpleHttpServer

我有带有“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 请求相同的请求

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 = []

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

在此先感谢您的帮助或线索。

解决方法:

如果您 运行 遇到这个问题,您可以在 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,强调我的)