欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)的登陸Discuz!論壇通用代碼分享

 更新時(shí)間:2014年07月11日 09:34:05   投稿:junjie  
這篇文章主要介紹了python實(shí)現(xiàn)的登陸Discuz!論壇通用代碼分享,需要的朋友可以參考下

代碼如下:

#coding:gbk
import urllib2,urllib,cookielib,re

'''
 通用的登陸DZ論壇
 參數(shù)說明parms:
   username:用戶名(必填),
   password :密碼(必填),
   domain:網(wǎng)站域名,注意格式必須是:http://www.xxx.xx/(必填),
   answer:問題答案,
   questionid:問題ID,
   referer:跳轉(zhuǎn)地址
   
 這里使用了可變關(guān)鍵字參數(shù)(相關(guān)信息可參考手冊(cè))
'''
def login_dz(**parms):

  #初始化
  parms_key = ['domain','answer','password','questionid','referer','username']
  arg = {}
  for key in parms_key:
    if key in parms:
      arg[key] = parms[key]
    else:
      arg[key] = ''
      
  #cookie設(shè)置
  cookieFile = './kan_cookies.dat'
  cookie = cookielib.LWPCookieJar()
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

  #獲取formhash
  pre_login = arg['domain']+'member.php?mod=logging&action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=fwin_content_login'
  c = opener.open(pre_login).read()
  cookie.save(cookieFile)
  patt = re.compile(r'.*?name="formhash".*?value="(.*?)".*?')
  formhash = patt.search(c)
  if not formhash:
    raise Exception('GET formhash Fail!')
  formhash = formhash.group(1)

  #登陸
  postdata = {
   'answer':arg['answer'],
   'formhash':formhash,
   'password':arg['password'],
   'questionid':0 if arg['questionid']=='' else arg['questionid'],
   'referer':arg['domain'] if arg['referer']=='' else arg['referer'],
   'username':arg['username'],
    }

  postdata = urllib.urlencode(postdata)
  req = urllib2.Request(
    url= arg['domain']+'member.php?mod=logging&action=login&loginsubmit=yes&handlekey=login&loginhash=LCaB3&inajax=1',
    data=postdata
    )
  c = opener.open(req).read(300)
  flag = '登陸失敗 %s'%arg['username']
  if 'succeedhandle_login' in c:
    flag = True
  return flag


#使用例子:基本參數(shù)登陸
user='xxx'
pwd='xxx'
dom='http://www.discuz.net/' #另一個(gè)測(cè)試網(wǎng)站:http://bbs.jb51.net/
try:
  flag = login_dz(username=user,password=pwd,domain=dom)
  print(flag)
except Exception,e:
  print('Error:',e)

相關(guān)文章

最新評(píng)論