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è)測試網(wǎng)站:http://bbs.jb51.net/
try:
flag = login_dz(username=user,password=pwd,domain=dom)
print(flag)
except Exception,e:
print('Error:',e)
您可能感興趣的文章:
- python模擬新浪微博登陸功能(新浪微博爬蟲)
- python使用paramiko模塊實(shí)現(xiàn)ssh遠(yuǎn)程登陸上傳文件并執(zhí)行
- Python爬蟲利用cookie實(shí)現(xiàn)模擬登陸實(shí)例詳解
- python實(shí)現(xiàn)帶驗(yàn)證碼網(wǎng)站的自動(dòng)登陸實(shí)現(xiàn)代碼
- python實(shí)現(xiàn)多線程暴力破解登陸路由器功能代碼分享
- python3.3教程之模擬百度登陸代碼分享
- Python實(shí)現(xiàn)SSH遠(yuǎn)程登陸,并執(zhí)行命令的方法(分享)
- 基于python的Tkinter編寫登陸注冊(cè)界面
- 利用selenium 3.7和python3添加cookie模擬登陸的實(shí)現(xiàn)
- python實(shí)現(xiàn)簡單登陸系統(tǒng)
相關(guān)文章
Python實(shí)現(xiàn)遍歷大量表格文件并篩選出數(shù)據(jù)缺失率低的文件
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)遍歷大量表格文件并篩選出表格內(nèi)數(shù)據(jù)缺失率低的文件的功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05
利用Python實(shí)現(xiàn)數(shù)值積分的方法
這篇文章主要介紹了利用Python實(shí)現(xiàn)數(shù)值積分。本文主要用于對(duì)比使用Python來實(shí)現(xiàn)數(shù)學(xué)中積分的幾種計(jì)算方式,并和真值進(jìn)行對(duì)比,加深大家對(duì)積分運(yùn)算實(shí)現(xiàn)方式的理解2022-02-02
python腳本調(diào)用iftop 統(tǒng)計(jì)業(yè)務(wù)應(yīng)用流量的思路詳解
這篇文章主要介紹了python腳本調(diào)用iftop 統(tǒng)計(jì)業(yè)務(wù)應(yīng)用流量的思路詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Python 16進(jìn)制與中文相互轉(zhuǎn)換的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Python 16進(jìn)制與中文相互轉(zhuǎn)換的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Python自動(dòng)化測試工具Splinter簡介和使用實(shí)例
這篇文章主要介紹了Python自動(dòng)化測試工具Splinter簡介和使用實(shí)例,Splinter可以非常棒的模擬瀏覽器的行為,Splinter提供了豐富的API,可以獲取頁面的信息判斷當(dāng)前的行為所產(chǎn)生的結(jié)果2014-05-05

