我开发了一个 PHP 脚本来在身份验证后传递一个文件。
<?php #SERVER.PHP
if (isset($_REQUEST['uname']) && isset($_REQUEST['passwd'])) {
if ($_REQUEST['uname']=='a' && $_REQUEST['passwd']=='a') {
session_start();
session_regenerate_id();
header('Content-Disposition: attachment; filename=fake_name.pdf');
readfile('original_name.pdf');
}
}
?>
<form name="login" action="test.php" method="get">
Username: <input type="text" name="uname"> <br />
Password: <input type="text" name="passwd"> <br />
<input type="submit" name="submit" />
</form>
因此,我想自动化登录和下载过程,最初我尝试使用 wget 下载文件(fake_name.pdf):
$ wget "http://1.1.1.1/server.php?uname=a&passwd=a"
但它下载了一个包含内容的文件
<form name="login" action="test.php" method="get">
Username: <input type="text" name="uname"> <br />
Password: <input type="text" name="passwd"> <br />
<input type="submit" name="submit" />
</form>
从网络浏览器访问时,我可以下载文件“mask_fname.pdf”工作得很好。
然后我尝试编写 python 脚本来获取文件,我只得到 HTML 内容。
#py1.py
import httplib, urllib
params = urllib.urlencode({
'uname' : 'a',
'passwd' : 'a'
})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn = httplib.HTTPConnection("10.1.1.2:80")
conn.request("GET", "/mdh/test.php?uname=a&passwd=a",
params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
print data
conn.close()
#py2.py
import urllib
import urllib2
url = 'http://10.1.1.2/mdh/index.php'
form_data = {'uname': 'a', 'passwd': 'a'}
params = urllib.urlencode(form_data)
response = urllib2.urlopen(url, params)
data = response.read()
print data
但是我所有尝试的输出都是相同的。有没有其他方法可以做到这一点。除了网络浏览器自动化(链接python :: splinter,selenium)之外,还有其他方法可以使用给定的用户名和密码自动下载文件(fake_name.pdf)吗?
最终,我需要使用带有身份验证的 HTTP 从服务器自动下载文件。