Discuz! X3.2 batch add your python script, x3.2python
Discuz! X3.2 add your python scripts in batches
Set up a Discuz! The X3.2 environment is used for work. When users need to be added, no proper tool is found. Although my colleagues finally solved the problem by pressing the key wizard, they decided to write a piece of code to stay, which may be useful.
The reason is that Python is not PHP, but Python is recently used, and it is more convenient to find it in this situation.
Add User code:
# Encoding: UTF-8 ''' Created on August 1 @ author: ZhongPing ''' import urllibimport urllib2import cookielibimport reclass Adder (object ): '''classdoc''' 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 Cookie self. cookieJar = cookielib. cookieJar () # instantiate a global opener self. opener = urllib2.build _ opener (urllib2.HTTPCookieProcessor (self. cookieJar) self. headers = {"Host": "localhost", "Referer": url} def login (self ): '''administrator logon system' # username and password data = {"admin_username": self. admin_user, "admin_password": self. admin_password, 'Frames ': 'yes', 'admin _ questionid': '0', 'submit ': 'Submit'} # urllib encoding 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', email1_y = '0', addsubmit = 'Submit '): ''' Add User ''' url = "" url = self. home_url + ('Action = members & operation = add') values = {'form Hash ': self. formhash, 'newusername': uname, 'newpassword': upwd, 'newemail': uemail, 'newgroupid': ugrpid, 'emailpolicy': emailpolicy, '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 ('
1. Modify the relevant parameters in the Main () function according to the actual situation:
File: a file that includes user information.
Home_url: Manage the access path of the background.
Pwd: the Administrator's access password.
2. User information files store user information in sequence(User name, password, email address, user group, whether to notify, addsubmit ). It can be organized in the following two ways:
Contains 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 only need some information:
test1,123456,test1@test.comtest2,123456,test2@test.comtest3,123456,test3@test.comtest4,123456,test4@test.com
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.