HTTP POST与饼干获得在蟒蛇认证(HTTP POST and GET with cookies

2019-06-23 19:20发布

我试图创建一个使用我的ID和密码登录到我的大学的网站Python程序。 这是用于登录正规页: https://webapp.pucrs.br/consulta/

正如你可能会注意到,这两个字段命名为PR1和PR2。 该网页使用POST发送数据。 此外,还有在加载页面时下载一个cookie,它包含的是,我的理解,你必须对POST方法来认证登录的头部返回一个随机值JSESSIONID。

我写了下面的代码,但在GET方法返回的页面上显示“会议未初始化”,很可能导致该Cookie不送回正确。

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所以登录被验证?

Answer 1:

我会尝试使用requests库。 该文档是优秀的,并且代码最终被比更清洁urllib*

$ pip install requests

使用会话来处理它自己的cookies(见彼得评论),结果是这样的

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 inexistente”通知pr1 123456789和“Sehnainválida”与您的用户数。



Answer 2:

你必须使用相同的“开门红”已为所有请求创建,将全部由自己处理的cookie。

这里是我最近写的东西摘录

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))

# then for all requests

if postData:     
    pData =  urllib.urlencode(postData)
else:
    pData = None

httpReq = urllib2.Request(url, pData, self._headers)
page =  opener.open(httpReq)


Answer 3:

转换MatthieuW的回答到Python 3给出。

import urllib, http.cookiejar

opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))
# then for all requests

if postData:     
    pData =  urllib.parse.urlencode(postData)
else:
    pData = None

httpReq = urllib.request.Request(url, pData)
page =  opener.open(httpReq)


Answer 4:

我建议你使用机械化 ,它会自动为你处理会话/饼干/登录,此外,它提供了一个的urllib般的API和如填表格,所以你不必用正确的POST请求的混乱,因为它得到通过机械化建设。



Answer 5:

urllib的是没有好,使用要求!

from requests import Request, Session

url = "https://webapp.pucrs.br/consulta/principal.jsp"
s = requests.Session()

p = dict(pb1 = 'dd', pb2 = 'cc')
r = s.get(url, params = p) 
# use the cert=/path/to/certificate.pem if you need it
# elsewhere use verify = False to bypass ssl verification

c = r.cookies

# Then send back a response using those same cookies

r = requests.get(other_url, cookies = c, verify = False)


文章来源: HTTP POST and GET with cookies for authentication in python