To discuz! X3.2 Adding a user's Python script in bulk
Built a discuz!. X3.2 environment for work, need to add users did not find the right tool, although colleagues finally solved the problem with the key wizard, but decided to write a piece of code to stay, perhaps the use.
Python, rather than PHP, is only recently in contact with Python, and is found to be more convenient on this occasion.
Add user code:
# Encoding:utf-8"' Created on July 1, 2015 @author:zhongping 'ImportUrllibImportUrllib2ImportCookielibImportRe class 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# Initialize a cookiejar to process cookiesSelf.cookiejar=cookielib. Cookiejar ()# Instantiate a global openerSelf.opener=urllib2.build_opener (URLLIB2. Httpcookieprocessor (Self.cookiejar)) Self.headers ={"Host":"localhost","Referer": URL} def login(self): " Admin Login System" # Login username and passworddata={"Admin_username": Self.admin_user,"Admin_password": Self.admin_password,' Frames ':' yes ',' Admin_questionid ':' 0 ',' Submit ':' Submit '}# Urllib to encodePost_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: -+i] pattern = re.compile (R ' <input type= "hidden" name= "Formhash" value= "(\w+)"/> "Match = Pattern.match (tpage) Formhash ="' ifMatch:formhash = Match.Groups () [0] Self.formhash = Formhash#print (Self.formhash) def adduser(self,uname,upwd,uemail,ugrpid = ' Ten ', emailnotify = ' 0 ', Addsubmit = ' Submit '): " Add User"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_pa Ge.find (' )if(i>0): Print ("User"+uname+"added successfully! "). Decode ("UTF8"))Else: Print ("User"+uname+"Add failed! "). Decode ("UTF8")) def addusers(self,users): "' Bulk Add Users: [{' Newusername ': newusername, ' newpassword ': NewPassword, ' Newemail ': newemail, ' newgroupid ': ' Ten ', ' emailnotify ': ' 0 ', ' addsub MIT ': ' Addsubmit '}, ....] " "Self.login () forUinchUsersif(Hasattr (U,"Newgroupid") andHasattr (U,"Emailnotify") andHasattr (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 () forLinchLinesifLen (L) >0: U = l.split (",")ifLen (u) = =6: Users.append ({' Newusername ': u[0],' NewPassword ': u[1],' Newemail ': u[2],' Newgroupid ': u[3],' emailnotify ': u[4],' Addsubmit ': u[5] })ifLen (u) = =3: Users.append ({' Newusername ': u[0],' NewPassword ': u[1],' Newemail ': u[2] })returnUsers 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. In the main () function, the relevant parameters need to be modified according to the actual situation:
File: Files that include user information.
Home_url: Manage access paths in the background.
PWD: Administrator's access password.
2. User information files in order to store user information (user name, password, mailbox, user group, whether notified, Addsubmit). Can be organized as follows two ways:
Include all information:
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
You can also just need some information:
test1,123456,test1@test.comtest2,123456,test2@test.comtest3,123456,test3@test.comtest4,123456,test4@test.com
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
To discuz! X3.2 Adding a user's Python script in bulk