標籤:python
為Discuz! X3.2大量新增使用者的python指令碼
搭建了一個Discuz! X3.2環境用於工作,需要添加使用者時沒有找到合適的工具,雖然同事最終用按鍵精靈解決了問題,不過還是決定寫一段代碼留下來,也許用得上。
之所以是Python而不是PHP,只是最近在接觸Python,發現在這種場合更加方便。
添加使用者代碼:
# encoding: utf-8‘‘‘Created on 2015年7月1日@author: ZhongPing‘‘‘import urllibimport urllib2import cookielibimport reclass Adder(object): ‘‘‘ classdocs ‘‘‘ home_url = ‘‘ admin_user = ‘‘ admin_password = ‘‘ formhash = ‘‘ def __init__(self, url, admin_user, admin_password): ‘‘‘ Constructor ‘‘‘ self.home_url = url + "?" self.admin_user = admin_user self.admin_password = admin_password # 初始化一個CookieJar來處理Cookie self.cookieJar=cookielib.CookieJar() # 執行個體化一個全域opener self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookieJar)) self.headers ={ "Host":"localhost", "Referer": url } def login(self): ‘‘‘ 管理員登入系統 ‘‘‘ # 登陸使用者名稱和密碼 data={ "admin_username":self.admin_user, "admin_password":self.admin_password, ‘frames‘:‘yes‘, ‘admin_questionid‘:‘0‘, ‘submit‘:‘提交‘ } # urllib進行編碼 post_data=urllib.urlencode(data) url = self.home_url req=urllib2.Request(url,post_data,self.headers) result = self.opener.open(req) url = self.home_url+‘action=members&operation=add‘ req=urllib2.Request(url) result = self.opener.open(req) tpage = result.read() i = tpage.find(‘<input type="hidden" name="formhash" value="‘) tpage = tpage[i:100+i] pattern = re.compile(r‘<input type="hidden" name="formhash" value="(\w+)" />‘) match = pattern.match(tpage) formhash = ‘‘ if match: formhash = match.groups()[0] self.formhash = formhash #print(self.formhash) def adduser(self,uname,upwd,uemail,ugrpid = ‘10‘,emailnotify = ‘0‘,addsubmit = ‘提交‘): ‘‘‘ 添加使用者 ‘‘‘ url = "" url = self.home_url+(‘action=members&operation=add‘) values = {‘formhash‘:self.formhash, ‘newusername‘:uname, ‘newpassword‘:upwd, ‘newemail‘:uemail, ‘newgroupid‘:ugrpid, ‘emailnotify‘:emailnotify, ‘addsubmit‘:addsubmit } data = urllib.urlencode(values) req=urllib2.Request(url,data,self.headers) response = self.opener.open(req) the_page = response.read() i = the_page.find(‘<h3>Discuz! 提示</h3><div class="infobox"><h4 class="infotitle2">使用者‘) if (i>0): print(("使用者"+uname+"添加成功!").decode("utf8")) else: print(("使用者"+uname+"添加失敗!").decode("utf8")) def addusers(self,users): ‘‘‘ 大量新增使用者 users : [{‘newusername‘:newusername, ‘newpassword‘:newpassword, ‘newemail‘:newemail, ‘newgroupid‘:‘10‘, ‘emailnotify‘:‘0‘, ‘addsubmit‘:‘addsubmit‘ }, ....] ‘‘‘ self.login() for u in users: if (hasattr(u, "newgroupid") and hasattr(u, "emailnotify") and hasattr(u, "addsubmit")) : self.adduser(u[‘newusername‘], u[‘newpassword‘], u[‘newemail‘], u[‘newgroupid‘], u[‘emailnotify‘], u[‘addsubmit‘]) else: self.adduser(u[‘newusername‘], u[‘newpassword‘], u[‘newemail‘])def readtxt(file): users = [] fo = open(file) lines = fo.readlines() for l in lines: if len(l)>0 : u = l.split(",") if len(u) == 6: users.append({‘newusername‘:u[0], ‘newpassword‘:u[1], ‘newemail‘:u[2], ‘newgroupid‘:u[3], ‘emailnotify‘:u[4], ‘addsubmit‘:u[5] }) if len(u) == 3: users.append({‘newusername‘:u[0], ‘newpassword‘:u[1], ‘newemail‘:u[2] }) return users def main(): file = ‘user.txt‘ home_url = ‘http://localhost/upload/admin.php‘ admin = ‘admin‘ pwd = ‘123456‘ adder = Adder(home_url,admin,pwd) users = readtxt(file) adder.addusers(users)if __name__ == ‘__main__‘: main() pass
1. 在Main()函數中需要根據實際情況修改相關參數:
file:包括使用者資訊的檔案。
home_url:管理背景訪問路徑。
pwd:管理員的訪問密碼。
2.使用者資訊檔按順序儲存使用者資訊(使用者名稱,密碼,郵箱,使用者組,是否通知,addsubmit)。可以按如下兩種方式組織:
包含全部資訊:
test1,123456,test1@test.com,10,0,addsubmittest2,123456,test2@test.com,10,0,addsubmittest3,123456,test3@test.com,10,0,addsubmittest4,123456,test4@test.com,10,0,addsubmit
也可以只需要部分資訊:
test1,123456,test1@test.comtest2,123456,test2@test.comtest3,123456,test3@test.comtest4,123456,test4@test.com
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
為Discuz! X3.2大量新增使用者的python指令碼