python實現的登陸Discuz!論壇通用代碼分享

來源:互聯網
上載者:User
代碼如下:

#coding:gbkimport urllib2,urllib,cookielib,re''' 通用的登陸DZ論壇 參數說明parms:   username:使用者名稱(必填),   password :密碼(必填),   domain:網站網域名稱,注意格式必須是:http://www.xxx.xx/(必填),   answer:問題答案,   questionid:問題ID,   referer:跳轉地址    這裡使用了可變關鍵字參數(相關資訊可參考手冊)'''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設定  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#使用例子:基本參數登陸user='xxx'pwd='xxx'dom='http://www.discuz.net/' #另一個測試網站:http://bbs.bitsCN.com/try:  flag = login_dz(username=user,password=pwd,domain=dom)  print(flag)except Exception,e:  print('Error:',e)
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.