gpt4 book ai didi

python-3.x - python 3.5 urllib.request.Request POST 数据到网站。执行 GET 而不是 POST

转载 作者:行者123 更新时间:2023-12-03 19:06:35 25 4
gpt4 key购买 nike

在 Python Urllib 中执行 POST 而不是 GET 时遇到问题。我正在运行 3.5。我正在尝试 POST 以形成字段。

我读到如果存在数据参数,urllib.request.Request 将默认为 POST。我在 https://docs.python.org/3/howto/urllib2.html 阅读了这篇文章

我复制了这些设置,当我启动 wireshark 时,我看到的只是 GET 而不是 Post,即使看起来代码正在执行。

这是我的代码:

values = {"field1" : z[2:-1], "Submit":"Save"}
print(values)
data = urllib.parse.urlencode(values)
data = data.encode('utf-8')
print(data)
req = urllib.request.Request("http://www.randomsite.com/myprocessingscript.php", data)
with urllib.request.urlopen(req) as response:
the_page = response.read()
print(the_page)

当我启动 wireshark 时,这是请求行的结果:

获取/myprocessingscript.php HTTP/1.1接受编码:身份主机:ec2-52-91-45-113.compute-1.amazonaws.com连接:关闭用户代理:Python-urllib/3.5

HTTP/1.1 200 正常日期:2015 年 10 月 28 日星期三 02:47:22 GMT服务器:Apache/2.4.17 (Unix) OpenSSL/1.0.1p PHP/5.5.30 mod_perl/2.0.8-dev Perl/v5.16.3X-Powered-By: PHP/5.5.30内容长度:23连接:关闭内容类型:文本/html

没有要处理的发布数据

另外,当我运行脚本时,这是我从打印语句中得到的:

{'提交': '保存', 'field1': 'hoSTLab\chris'}b'提交=保存&field1=hoSTLab%5Cchris%5Cr%5Cn'b'没有要处理的发布数据'追溯(最近一次通话): 文件“C:\Users\chris\Desktop\test.py”,第 20 行,位于 时间. sleep (随机.randint(5,10))

他们正在访问两个网络文件。 Index.html 和 myprocessingscript.php:

Index.html:

<h1>randomsite.com.</h1>

####<p>whoami</p>

<form action="myprocessingscript.php" method="POST">
<input name="field1" type="text" />
<input type="submit" name="submit" value="Save">
</form>

</body>
</html>

我的processingscript.php:

<?php if(isset($_POST['field1'])) {
$data = $_POST['field1'] . "\n";
$ret = file_put_contents('/tmp/mydata.txt', $data);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}

最佳答案

HTTP POST 按预期工作:

#!/usr/bin/env python
from contextlib import closing
try:
from urllib.parse import urlencode
from urllib.request import urlopen
except ImportError: # Python 2
from urllib import urlencode
from urllib2 import urlopen

url = 'http://httpbin.org/post'
data = urlencode({"field1" : "value", "Submit": "Save"}).encode()
with closing(urlopen(url, data)) as response:
print(response.read().decode())

只有在 http 重定向 ( as the rfc recommends -- no data should be posted on redirect without prompting the user ) 之后,您才会看到 GET

例如,这是一个重定向 POST / 请求的 http 服务器:

#!/usr/bin/env python
from flask import Flask, redirect, request, url_for # $ pip install flask

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
return redirect(url_for('post'))
return '<form method="POST"><input type="submit">'


@app.route('/post', methods=['GET', 'POST'])
def post():
return 'Hello redirected %s!' % request.method

if __name__ == '__main__':
import sys
port = int(sys.argv[1]) if len(sys.argv) > 1 else None
app.run(host='localhost', port=port)

使用相同的代码 (urlopen(url, data)) 发出 HTTP POST 请求会导致重定向,第二个请求是 GET:

"POST / HTTP/1.1" 302 -
"GET /post HTTP/1.1" 200 -

同样,第一个请求是POST,而不是GET。如果您访问 / 并单击 submit 按钮(浏览器先发出 POST 请求,然后发出 GET 请求),则行为完全相同。

Python issue: "Document how to forward POST data on redirects"包含指向 HTTPRedirectHandler's subclass that posts data on redirect 的链接:

#!/usr/bin/env python
from contextlib import closing
try:
from urllib.parse import urlencode
from urllib.request import (HTTPError, HTTPRedirectHandler, Request,
build_opener, urlopen)
except ImportError: # Python 2
from urllib import urlencode
from urllib2 import (HTTPError, HTTPRedirectHandler, Request,
build_opener, urlopen)

class PostHTTPRedirectHandler(HTTPRedirectHandler):
"""Post data on redirect unlike urrlib2.HTTPRedirectHandler."""
def redirect_request(self, req, fp, code, msg, headers, newurl):
m = req.get_method()
if (code in (301, 302, 303, 307) and m in ("GET", "HEAD")
or code in (301, 302, 303) and m == "POST"):
newurl = newurl.replace(' ', '%20')
CONTENT_HEADERS = ("content-length", "content-type")
newheaders = dict((k, v) for k, v in req.headers.items()
if k.lower() not in CONTENT_HEADERS)
return Request(newurl,
data=req.data,
headers=newheaders,
origin_req_host=req.origin_req_host,
unverifiable=True)
else:
raise HTTPError(req.get_full_url(), code, msg, headers, fp)


urlopen = build_opener(PostHTTPRedirectHandler).open

url = 'http://localhost:5000'
data = urlencode({"field1" : "value", "Submit": "Save"}).encode()
with closing(urlopen(url, data)) as response:
print(response.read().decode())

在这种情况下,访问日志显示了两个 POST 请求(第二个请求是 POST):

"POST / HTTP/1.1" 302 -
"POST /post HTTP/1.1" 200 -

注意:您可以自定义 HTTPRedirectHandler 以遵循 rfc 2616 behavior .

关于python-3.x - python 3.5 urllib.request.Request POST 数据到网站。执行 GET 而不是 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33382273/

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