问题
I am trying to do a program in Python which logs in to a vBulletin forum.
I need to access only one page, but in order to view that page I must be logged in.
Here is my code:
#-*- coding:utf-8 -*-
import urllib, urllib2, cookielib, hashlib, time
def variables():
domain = "www.example.com"
uname = "UserName"
passwd = "Password"
# Create url
if domain.startswith('http://'):
url = domain
else:
url = 'http://' + domain
login(url, uname, passwd)
def login(url, uname, passwd):
loginurl = url + '/login.php?do=login'
md5 = hashlib.md5(passwd);md5 = md5.hexdigest()
# Options for request
opts = {
'do': 'login',
'vb_login_md5password': md5,
'vb_login_md5password_utf': md5,
's': '',
'vb_login_username': uname,
'security_token': 'guest',
}
data = urllib.urlencode(opts)
# Request header
global headers
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'es-es,es;q=0.8,en-us;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip,deflate',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Connection': 'keep-alive',
'Referer': loginurl,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Lenght': '205'
}
# Cookie Handling
jar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
# Send Request
opener.addheader = headers
opener.open(loginurl, data)
# Check
response = opener.open('http://example.com/usercp.php?variable')
print response.read()
variables()
The problem is that it doesn't log me in and I can't find the reason why.
回答1:
1) you need to send vb_login_name and vb_login_password to login.php?do=login
2) you need to send below values to login.php?do=login too (view html code)
<input type="hidden" name="s" value="215c619cb0227a30bab788ca0adb1c42" />
<input type="hidden" name="securitytoken" value="guest" />
<input type="hidden" name="do" value="login" />
<input type="hidden" name="vb_login_md5password" />
below line means you shoud md5hash the vb_login_password, vb_login_md5password, vb_login_md5password_utf before you send these values to login.php
<form action="login.php?do=login" method="post" onsubmit="md5hash(vb_login_password, vb_login_md5password, vb_login_md5password_utf, 0)">
HTTP POST data: username=abc, password=123
POST /forums/login.php?do=login HTTP/1.1
Host: absolutewrite.com
Connection: keep-alive
Content-Length: 216
Cache-Control: max-age=0
Origin: http://absolutewrite.com
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.0 Safari/535.11
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://absolutewrite.com/forums/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: UTF-8,*;q=0.5
Cookie: bbsessionhash=215c619cb0227a30bab788ca0adb1c42; bblastvisit=1323333120; bblastactivity=0
vb_login_username=abc&vb_login_password=&s=215c619cb0227a30bab788ca0adb1c42&securitytoken=guest&do=login&vb_login_md5password=202cb962ac59075b964b07152d234b70&vb_login_md5password_utf=202cb962ac59075b964b07152d234b70
回答2:
opts = {
'do': 'login',
'vb_login_md5password': md5,
'vb_login_md5password_utf': md5,
'vb_login_password':'',
's': '215c619cb0227a30bab788ca0adb1c42',
'vb_login_username': uname,
'security_token': 'guest',
}
来源:https://stackoverflow.com/questions/8427289/logging-in-to-vbulletin