Login Discuz implemented by python! Common Forum code sharing

Source: Internet
Author: User
This article describes how to log on to Discuz through python! Share the common code of the Forum. For more information, see the following code:

# Coding: gbkimport urllib2, urllib, cookielib, and re''' common DZ login Forum parameter descriptions parms: username: User Name (required), password: password (required ), domain: the website domain name. Note that the format must be: http://www.xxx.xx/ (Required), answer: answer to the question, questionid: Question ID, referer: Jump address variable keyword parameters are used here (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: arg [key] = ''# cookie setting cookieFile = '. /kan_cookies.dat 'cookie = cookielib. LWPCookieJar () opener = urllib2.build _ opener (urllib2.HTTPCookieProces Sor (cookie) # Get 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: basic Parameter Login user = 'xxx' pwd = 'xxx' dom =' http://www.discuz.net/ '# Another test Website: http://bbs.bitsCN.com/try : Flag = login_dz (username = user, password = pwd, domain = dom) print (flag) failed t Exception, e: print ('error: ', e)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.