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) 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)
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
HTTP POST data: username=abc, password=123