1、方式一,获取session后。存储起来。下次调用时候,传递一个session植即可
#/bin/python # -*- coding: utf-8 -*- import requests from urllib import parse class HarborAapi(object): def __init__(self,url,username,passwd): ''' @Author: xiajinqi :param url: :param username: :param passwd: ''' print("构造函数") self.url = url self.username= username self.passwd=passwd self.session_id_key = "sid" def login_get_session(self): print("获取session") header={ 'User-Agent':'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36','Content-Type': 'application/x-www-form-urlencoded' } req_url='%s/c/login'%(self.url) req_dict={ 'principal': self.username, 'password': self.passwd } req_data=parse.urlencode(req_dict) try : res = requests.post(req_url, headers=header, data=req_data) print(res.text) if res.status_code==200 : ### 200位数字不可加" self.session_id = res.cookies.get("sid") return self.session_id except : raise Exception("login error,please check your account info!" ) def get_projects(self): print("获取工程") header = { 'User-Agent': 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36', 'Content-Type': 'application/json' } req_url = '%s/api/projects?page=1&page_size=15' % (self.url) req_dict={ 'page': '1', 'page_size': '15' } r = requests.get(req_url, cookies={'sid':self.login_get_session()}) print(r.text) print(r.status_code) ### cookies={self.session_id_key: self.session_id} def logout(self): r=requests.get('%s/c/logout' % (self.url),cookies={'sid': self.session_id}) print(r.text) harbor=HarborAapi('http://xx.xxxxx.com','test','00000') print(harbor.login_get_session()) harbor.get_projects()
方式二通过session 方式发送请求
#/bin/python # -*- coding: utf-8 -*- import requests from urllib import parse class HarborAapi(object): def __init__(self,url,username,passwd): ''' @Author: xiajinqi :param url: :param username: :param passwd: ''' print("构造函数") self.url = url self.username= username self.passwd=passwd self.session_id_key = "sid" def login_get_session(self): print("获取session") header={ 'User-Agent':'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36','Content-Type': 'application/x-www-form-urlencoded' } req_url='%s/c/login'%(self.url) req_dict={ 'principal': self.username, 'password': self.passwd } req_data=parse.urlencode(req_dict) try : s = requests.Session() ###通过session方式请求。会自动保留cookie信息 res = s.post(req_url, headers=header, data=req_data) print(res.text) if res.status_code==200 : ### 200位数字不可加" self.session=s # self.session_id = res.cookies.get("sid") print(s) return s #####s中存在cookie植 except : raise Exception("login error,please check your account info!" ) def get_projects(self): print("获取工程") header = { 'User-Agent': 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36', 'Content-Type': 'application/json' } req_url = '%s/api/projects?page=1&page_size=15' % (self.url) req_dict={ 'page': '1', 'page_size': '15' } r = self.session.get(req_url) print(r.text) print(r.status_code) ### cookies={self.session_id_key: self.session_id} def logout(self): r=requests.get('%s/c/logout' % (self.url),cookies={'sid': self.session_id}) print(r.text) harbor=HarborAapi('https:/******','test','1234') harbor.login_get_session() harbor.get_projects()
### 返回session对象
构造函数
获取session
<requests.sessions.Session object at 0x0000000003F0BE80>
方式三:通过auth 和session 相互结合的模式来实现,跨路径访问
#/bin/python # -*- coding: utf-8 -*- import requests from urllib import parse from requests.auth import HTTPBasicAuth class HarborAapi(object): def __init__(self,url,auth): ''' @Author: xiajinqi :param url: ''' print("构造函数") self.url = url self.auth=auth self.session_id_key = "sid" def setting(self): ###定义会话保持s并且设置s的auth 属性 self.session=requests.session() self.session.auth=self.auth def get_projects(self): print("获取工程") header = { 'User-Agent': 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36', 'Content-Type': 'application/json' } #https://harbor.yylending.com:8443/api/projects?page=1&page_size=15 req_url = '%s/api/projects?page=1&page_size=15' % (self.url) req_dict={ 'page': '1', 'page_size': '15' } req_data=parse.urlencode(req_dict) r = self.session.get(req_url,data=req_dict,headers=header) print(r.text) print(r.status_code) auth = HTTPBasicAuth('att','12345') harbor=HarborAapi('https://xxx.xxxx.com:44',auth) harbor.setting() ### harbor.get_projects()