用python登录Discuz7.2
时间:2010-09-01 来源:极品黑公子
#encoding=utf-8
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.phpchina.com/logging.php?action=login').read()
formhash = re.findall('<input type="hidden" name="formhash" value=(.*?) />', loginpage.decode("utf-8"), re.DOTALL) params = { 'formhash':formhash[0],
'username':self.name,
'password':self.pwd,
'loginsubmit':'true'
}
print 'login......'
self.request=urllib2.Request('http://bbs.phpchina.com/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("重整河山待后生",'******')
ren.login()
ren.open('http://bbs.phpchina.com/')
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.phpchina.com/logging.php?action=login').read()
formhash = re.findall('<input type="hidden" name="formhash" value=(.*?) />', loginpage.decode("utf-8"), re.DOTALL) params = { 'formhash':formhash[0],
'username':self.name,
'password':self.pwd,
'loginsubmit':'true'
}
print 'login......'
self.request=urllib2.Request('http://bbs.phpchina.com/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("重整河山待后生",'******')
ren.login()
ren.open('http://bbs.phpchina.com/')
相关阅读 更多 +