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

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

问题描述

我想创建一个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放在哪里, p>

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

推荐答案

我会尝试使用请求库。 文档非常出色,代码最终比使用<$ c $更清晰c> 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.

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

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