问题描述
我试图创建一个使用我的ID和密码登录到我的大学的网站python程序。这是为在记录正规页:
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,它包含的是,我的理解,你必须对POST方法来验证登录的头部返回一个随机值JSESSIONID。
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.
我写了下面code,但对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?
推荐答案
我会尝试使用要求
库。该是优秀的,而code最终被比更清洁的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
使用处理饼干(见彼得评论)对自己,结果看起来像这样
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
AUsuario inexistente通知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饼干在蟒蛇认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!