python爬虫CAS认证

import requests
from lxml import etree
from bs4 import BeautifulSoup
import re
import os
import pickle

class casService(object):
    def __init__(self,svr_session):
        self.cas_url = ""
        self.svr_session = svr_session  #service_session
        self.session = requests.session() #cas session
        # self.load_cascookies_from_file() #使用已有的cas-cookie(如果有的话)
        self.headers = {
                "Accept": "text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8",
                "Accept-Language": "zh_CN",
                "Connection": "keep-alive",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363",
            }
    def Login(self,serviceUrl = "",username = None,password = None):        
        response = self.svr_session.get(url=serviceUrl, headers = self.headers, allow_redirects=False)
        if response.status_code == 200:
            return True
        self.cas_url = response.headers["Location"]    
        cas_url = response.headers["Location"]
        cas_response = self.svr_session.get(cas_url, headers = self.headers, allow_redirects = False)
        if cas_response.status_code == 200:#登录界面
            if username == None or password == None:
                print("cas_cookie not valid")
                username = input("plase input username:")
                password = input("plase input password:")
            # loginhtml = etree.HTML(cas_response.text)
            loginhtml = BeautifulSoup(cas_response.text,'lxml')
            # execution_value = loginhtml.xpath("//input[@name='execution']/@value")
            execution_value = loginhtml.select('#fm1 > input[name=execution]')[0]['value']
            # lt_value = loginhtml.xpath("//div[@id='bottom']/input[@name='lt']/@value")
            auth_data =  {                
                "_eventId" : "submit",                
                "execution" : execution_value,
                "username" : username,
                "password" : password,                
                "loginType" : 1,
                "submit": "登  录"
            }
            auth_response = self.svr_session.post(self.cas_url,data = auth_data, headers = self.headers, allow_redirects = False)
            if auth_response.status_code == 302:
                url_with_ticket = auth_response.headers["Location"]
                confirm_response = self.session.get(url = url_with_ticket, headers = self.headers, allow_redirects = True)
                if confirm_response.status_code == 200:
                    print("logon on success")
                    # self.write_cascookies_to_file()
                    return requests.utils.dict_from_cookiejar(self.session.cookies)
                else:
                    print("logon on failed")
            else:
                print('auth failed')
                return False
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值