python 3.5 urlib.request.向网站请求POST数据.执行GET和NOT POST



在Python Urlib中执行POST而不是GET时遇到问题。我跑3.5。我正在尝试POST表单字段。

我读到,如果存在数据参数,urlib.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时,这是req行的结果:

GET/myprocessingscript.php HTTP/1.1接受编码:标识主持人:ec2-52-91-45-113.3compute-1.amazonaws.com连接:关闭用户代理:Python urllib/3.5

HTTP://1.1 200 OK日期:2015年10月28日星期三格林尼治标准时间02:47:22服务器:Apache/2.4.17(Unix)OpenSSL/1.0.1p PHP/5.3.50 mod_perl/2.0.8-dev perl/v5.16.3X-Powered-Bor:PHP/5.3.50内容长度:23连接:关闭内容类型:text/html/

没有要处理的后期数据

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

{"提交":"保存","字段1":"hostlab\chris"}b'Submit=保存&field1=主机实验室%5Chris%5Cr%5Cn'b'没有要处理的发布数据'追踪(最近一次通话):文件"C:\Users\chrs\Desktop\test.py",第20行,位于时间睡眠(randint(5,10))

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

索引.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');
}

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重定向后才能看到GET(正如rfc所建议的那样——在重定向时不应提示用户就发布任何数据)。

例如,这里有一个重定向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))发出HTTPPOST请求会导致重定向,第二个请求是GET:

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

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

Python问题:"文档如何在重定向上转发POST数据"包含指向HTTPRedirectHandler的子类的链接,该子类在重定向上发布数据:

#!/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以遵循rfc2616行为。

好的,所以我发现了问题所在。如果url是重定向的,那么python模块"requests.post"将不会执行post。所以我必须把实际的网址放进去才能工作,而不是一个能把我引导到我想要的网址的网址。

这与使用urllib 的用户相同

最新更新