python - 为什么我无法使用 Python "requests"库填写此表单?

标签 python python-2.7 python-requests

我想使用 Python 中的 requests 库填写登录表单。如下所示,字段名称为 usernamepassword : enter image description here

所以我编写了以下脚本:

session = requests.session()
p = session.post("http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx", {'username':'myUsername','password':'myPassword'})
print 'headers', p.headers
print 'cookies', requests.utils.dict_from_cookiejar(session.cookies)
print 'html',  p.text

问题是,当我运行它时,它再次返回登录页面!我的程序出了什么问题?

更新:

我也尝试将参数作为数据发送,但没有任何变化。即下面的代码也返回相同的输出:

payload = {'username': 'myUsername', 'password': 'myPassword'}
r = requests.post("http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx", data=payload)
print(r.text)

更新2:

我使用 Burp Suit 捕获数据包以查看差异:

这是我的浏览器发送的数据包:

POST /SubSystem/Portal/Index1/Login/Login2.aspx HTTP/1.1
Host: 192.168.94.1
Proxy-Connection: keep-alive
Content-Length: 134
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://192.168.94.1
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: ASP.NET_SessionId=nntwluu5uhrldzuymsv333uc; CURRENT_LANGUAGE_2=fa

__VIEWSTATE=%2FwEPDwUKMTY5ODAzNzY1MmRkpL4TXtJfODqn0yxypIJaOzkl4YUWynT8urN%2BYYXM%2FnY%3D&Command=LOGIN&username=myUsername&password=myPassword

这是第二个 Python 脚本发送的数据包:

POST /SubSystem/Portal/Index1/Login/Login2.aspx HTTP/1.1
Host: 192.168.94.1
Content-Length: 31
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.4.3 CPython/2.7.0 Windows/7
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded

username=myUsername&password=myPassword

最佳答案

看起来您首先需要发送一个 POST 请求,但从最近的更新来看,该 POST 还需要一些可用的 cookie - 还有一个名为“Command”的隐藏字段需要与值“LOGIN”一起发送,所以。

import requests

URL = "http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx"

# Set up session object so we can store cookies across requests
session = requests.session()
# GET the page to get any initial cookies required for the POST
r = session.get(URL)
# POST to the login form (making sure to add "Command")
r = session.post(URL, data={'Command': 'LOGIN', 'username': 'x', 'password': 'y'})

至于为什么在保存页面时没有获得图像,是因为当浏览器加载页面时,它会看到指向资源/样式表/图像的链接,并发出进一步的访问它们的请求 - 所有请求 所做的就是按原样加载页面的“文本”。有多种方法可以实现这一目标,但这超出了此处核心问题的答案范围。

<小时/>

根据关于我可以使用这些 session 对象进行多次登录吗?的评论 - 这是一种方法......

# List of users and passwords
USERS = {
    'rod': 'password1',
    'jane': 'password2',
    'freddy': 'password3'
}
# Initial name->initial session with cookies
sessions = {name:requests.session().get(URL) for name in USERS}
# Login users
sessions.update(
    (name, session.post(URL, data={'Command': 'LOGIN', 'username': name, 'password': USERS[name]})) 
    for name, session in sessions.items()
)
# Now do something with users
r1 = sessions['rod'].get('http://something.com')
r2 = sessions['freddy'].get('http://something.com', params={'foo': 'bar'})
# etc...

关于python - 为什么我无法使用 Python "requests"库填写此表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29076778/

相关文章:

python - 如何跟踪一段代码中变量的使用?

python - 从 Excel 到分割的 Python 结构

python - 为什么 *args 不适用于字符串格式

Python pexpect 返回命令和命令的输出

python - 请求无法连接到 TLS 服务器

python-3.x - 在python 3中使用requests.get获取数据之前等待页面加载

python - 操作系统错误 : Could not find a suitable TLS CA certificate bundle

Python 多重处理函数,将 header 添加到列表中的 URL

python - 找到数组元素的所有可能组合。不是产品

python - 如何在 tensorflow RNN 中使用 numpy 数组输入