玩蛇网提供最新Python编程技术信息以及Python资源下载!

自动登陆Discuz论坛的通用python代码源码片段

python 培训

自动登陆Discuz论坛的通用python代码源码片段,请大家注意自己的python版本,及 discuz 论坛版本!

使用例子:基本参数登陆参数设置

	user='xxx'
	pwd='xxx'
	dom='http://www.disscuz.net/'
	try:
	   flag = login_dz(username=user,password=pwd,domain=dom)
	   print(flag)
	except Exception,e:
	   print('Error:',e)

自动登陆Discuz论坛的通用python代码源码片段如下:

#coding:gbk
import urllib2,urllib,cookielib,re

'''
  通用的登陆DZ论坛
  参数说明parms:
      username:用户名(必填),
      password :密码(必填),
      domain:网站域名,注意格式必须是:http://www.xxx.xx/(必填),
      answer:问题答案,
      questionid:问题ID,
      referer:跳转地址
      
  这里使用了可变关键字参数
www.iplaypython.com
'''
def login_dz(**parms):

    #初始化
    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设置
    cookieFile = './kan_cookies.dat'
    cookie = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

    #获取formhash
    pre_login = arg['domain']+'member.php?mod=loggin
2000
g&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)

    #登陆
    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 = '登陆失败 %s'%arg['username']

    if 'succeedhandle_login' in c:
        flag = True
    return flag


#使用例子:基本参数登陆
user='xxx'
pwd='xxx'

dom='http://www.discuz.net/' 

try:
    flag = login_dz(username=user,password=pwd,domain=dom)
    print(flag)

except Exception,e:
    print('Error:',e)

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/other/o2371.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-14 11:15 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部