#encoding=gbk
import sys
import re
import cookielib
import urllib2
import urllib
import md5
class Discuz(object):
def __init__(self):
self.name=self.pwd=self.operate=""
self.cj=cookielib.CookieJar()
self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(self.opener)
self.opener.addheaders = [('User-agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)')]
def add_user_info(self,username,pwd):
self.name=username
self.pwd=pwd
def login(self):
m = md5.new()
m.update(self.pwd)
self.pwd=m.hexdigest()
loginpage = self.opener.open('http://bbs.antiy.cn/logging.php?action=login').read()
loginpage = self.opener.open('http://bbs.antiy.cn/logging.php?action=login').read()
formhash = re.findall('<input type="hidden" name="formhash" value=(.*?) />', loginpage.decode("gbk"), re.DOTALL)
params = { 'formhash':formhash[0],
'username':self.name,
'password':self.pwd,
'loginsubmit':'true'
}
print 'login......'
self.request=urllib2.Request('http://bbs.antiy.cn/logging.php?action=login&loginsubmit=yes&inajax=1',urllib.urlencode(params))
try:
req = self.opener.open(self.request)
page = req.read()
if page.find("欢迎"):
print "login sucess!"
except Exception,e:
print e
print urllib2.HTTPError
def open(self,url):
try:
page = urllib2.urlopen(url).read()
print page
except Exception,e:
print e
if __name__=='__main__':
ren=Discuz()
ren.add_user_info("your username",'your password')
ren.login()
ren.open(http://bbs.antiy.cn/index.html)
|