Login Discuz implemented by python! Common Forum code sharing, pythondiscuz
The Code is as follows:
# Coding: gbkimport urllib2, urllib, cookielib, and re''' common DZ login Forum parameter descriptions parms: username: User Name (required), password: password (required ), domain: website domain name, pay attention to the format must be: http://www.xxx.xx/(required), answer: answer, questionid: Question ID, referer: the redirection address uses variable keyword parameters (for more information, see the manual) '''def login_dz (** parms): # initialize parms_key = ['domain ', 'answer ', 'Password', 'questionid', 'Referer', 'username'] arg ={} for key in parms_key: if key in parms: arg [key] = parms [key] else: Rg [key] = ''# cookie setting cookieFile = '. /kan_cookies.dat 'cookie = cookielib. LWPCookieJar () opener = urllib2.build _ opener (urllib2.HTTPCookieProcessor (cookie) # obtain 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) # log on to 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 = 'login failed % s' % arg ['username'] if 'succeedhandle _ login' in c: flag = True return flag # example: log on to user = 'xxx' pwd = 'xxx' dom = 'HTTP: // www.discuz.net/'# Another test Website: http://bbs.jb51.net/try: flag = login_dz (username = user, password = pwd, domain = dom) print (flag) failed t Exception, e: print ('error: ', e)
Python implements web page login verification (automatic login), and has tried a lot of Baidu code (although only a few lines), that is, login fails. What is the problem?
Because the login form on this page is generated using javascript,
Except userName (not the username you wrote), password, and other fields,
You must also provide information about many hidden fields (including codeString, safeFlag, u, and isPhone)
The image is the source code of the login form. You must specify the hidden data in values to correctly simulate login behavior.
After studying the source code of the discuz Forum
There are a lot of articles on how UCenter can call databases online.