且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HTTP POST和GET使用Cookie在Python中进行身份验证

更新时间:2022-02-03 20:33:44

我会尝试使用请求库。 文档非常出色,代码最终比使用 urllib *

I would try using the requests library. The documentation is excellent, and the code ends up being much cleaner than with urllib*

$ pip install requests

使用会话(参见Piotr的评论),它自己处理cookie,结果如下

Using a session (see comment by Piotr) that handles cookies on its own, the result looks like this

import requests
url_0 = "http://webapp.pucrs.br/consulta/principal.jsp"
url = "https://webapp.pucrs.br/consulta/servlet/consulta.aluno.ValidaAluno"
data = {"pr1": "123456789", "pr2": "1234"}

s = requests.session()
s.get(url_0)
r = s.post(url, data)

看起来工作正常, Usuario不存在 pr1 123456789和Sehnainválida的通知。

It seems to work fine, as I get a "Usuario inexistente" notice for pr1 123456789 and "Sehna inválida" with your user-number.