在Python Urllib中执行POST而不是GET时遇到问题。我跑3.5。我试图POST以形成字段。
我读到如果数据参数存在,urllib.request.Request将默认为POST。我在https://docs.python.org/3/howto/urllib2.html
看了这个我复制了这些设置,当我启动wireshark时,我看到的是GET和Never a 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时,这是需求行的结果:
GET /myprocessingscript.php HTTP / 1.1 Accept-Encoding:身份 主持人:ec2-52-91-45-113.compute-1.amazonaws.com 连接:关闭 User-Agent: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.3 X-Powered-By:PHP / 5.5.30 内容长度:23 连接:关闭 内容类型:text / html
没有要处理的帖子数据
另外当我运行脚本时,这是我从打印语句中得到的结果:
{'提交':'保存',' field1':' hostlab \ chris'} B'提交=保存并FIELD1 = hostlab%5Cchris%5CR%5CN' b没有要处理的帖子数据' Traceback(最近一次调用最后一次): 文件" C:\ Users \ chris \ Desktop \ test.py",第20行,in time.sleep(random.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>
myprocessingscript.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');
}
答案 0 :(得分:5)
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。
答案 1 :(得分:0)
好的,所以我弄清楚出了什么问题。 python模块&#34; requests.post&#34;如果网址是重定向的网址,则不会执行帖子。所以我不得不把实际的网址用于工作,而不是一个会引导我到我想要的网址的网址。
对于那些使用urllib的人来说,这是一样的