使用 cookie 在 python 中进行身份验证的 HTTP POST 和 GET [英] HTTP POST and GET with cookies for authentication in python

查看:28
本文介绍了使用 cookie 在 python 中进行身份验证的 HTTP POST 和 GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 python 程序,该程序使用我的 ID 和密码登录到我大学的站点.这是登录的正式页面:https://webapp.pucrs.br/consulta/

I'm trying to create a python program that logs in to my university's site using my id and password. This is the formal page for logging in: https://webapp.pucrs.br/consulta/

您可能已经注意到,这两个字段被命名为 pr1 和 pr2.该页面使用 POST 发送数据.此外,页面加载时会下载一个 cookie,它是一个 JSESSIONID,其中包含一个随机值,据我所知,您必须在 POST 方法的标头上返回以验证登录.

As you may notice, the two fields are named pr1 and pr2. The page uses POST to send the data. ALSO, there's a cookie that is downloaded when the page is loaded, it's a JSESSIONID containing a random value that, as I understood, you have to return on the header of the POST method to authenticate the login.

我写了下面的代码,但是GET方法的返回页说会话没有初始化",可能是cookie没有正确发送回来.

I wrote the following code, but the return page on the GET method says "The session was not initialized", probably cause the cookie was not sent back properly.

from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
import httplib, urllib, cookielib, Cookie, os

conn = httplib.HTTPConnection('webapp.pucrs.br')

#COOKIE FINDER
cj = cookielib.CookieJar()
opener = build_opener(HTTPCookieProcessor(cj),HTTPHandler())
req = Request('http://webapp.pucrs.br/consulta/principal.jsp')
f = opener.open(req)
html = f.read()
for cookie in cj:
    c = cookie
#FIM COOKIE FINDER

params = urllib.urlencode ({'pr1':111049631, 'pr2':<pass>})
headers = {"Content-type":"text/html",
           "Set-Cookie" : "JSESSIONID=70E78D6970373C07A81302C7CF800349"}
            # I couldn't set the value automaticaly here, the cookie object can't be converted to string, so I change this value on every session to the new cookie's value. Any solutions?

conn.request ("POST", "/consulta/servlet/consulta.aluno.ValidaAluno",params, headers) # Validation page
resp = conn.getresponse()

temp = conn.request("GET","/consulta/servlet/consulta.aluno.Publicacoes") # desired content page
resp = conn.getresponse()

print resp.read()

我应该把这个 cookie 放在哪里以便登录被验证?

Where do I put this cookie so the login is authenticated?

推荐答案

我会尝试使用 requests 库.文档 非常好,代码最终比 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)

它似乎工作正常,因为我收到了 pr1 123456789 的Usuario inexistente"通知和带有您的用户号的Sehna inválida".

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

这篇关于使用 cookie 在 python 中进行身份验证的 HTTP POST 和 GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆