Python登錄接口如何獲取token并保存到文件中
Python登錄接口獲取token并保存到文件中
1.在項(xiàng)目下新建文件token.yaml文件,然后調(diào)用登錄接口
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 用pip3命令安裝
import requests
from ruamel import yaml
import json
def test_loginToGetToken():
host = 'http://xx.xx.xx.xx:xx/'#接口地址ip與port
url = host + "login"
#登錄的參數(shù)數(shù)據(jù)
data = {
'userName': '159592055xx',
'loginType': 2,
'password': '123123'
}
#登錄請(qǐng)求頭部信息
headers = {'Content-Type': 'application/json'}
# 初始化url請(qǐng)求對(duì)象
response = requests.post(url=url, data=json.dumps(data), headers=headers)
# print(response.text)
# print(response.status_code)
# print(response.json()["data"]["token"])
# return response.json()["token"]
# 把token值寫入配置文件中
yamlpath = r'D:\autotest\api\628x\Token.yaml'#保存文件路徑
#提取token字段
tokenValue = {
'token': response.json()["data"]["token"]
}
with open(yamlpath, "w", encoding="utf-8") as f:
yaml.dump(tokenValue, f, Dumper=yaml.RoundTripDumper)
if __name__ == "__main__":
test_loginToGetToken()2.查看token.yaml文件

Python自動(dòng)化實(shí)現(xiàn)獲取token
在公共函數(shù)模塊實(shí)現(xiàn)獲取token函數(shù),方便測(cè)試用例代碼實(shí)現(xiàn)時(shí)直接調(diào)用拿到token值。
1、使用的模塊
requests:第三方模塊,用來發(fā)送http請(qǐng)求和獲取返回的結(jié)果。
2、使用的方法
(1)requests的帶參數(shù)get請(qǐng)求
requests.get(url='',params={' ‘:' ‘,' ‘:' ‘})(2)或:字典類型的post請(qǐng)求方法
requests.post(url,data={' ‘:' ‘,' ‘:' '})(3)獲取響應(yīng)內(nèi)容
json():以json格式獲取接口響應(yīng)內(nèi)容
(4)獲取響應(yīng)內(nèi)容中的token值
json()[“data”][“token”]
或:
json().get(‘data').get(‘token')
響應(yīng)格式:

3、代碼實(shí)現(xiàn)
import requests
def getToken(): # 獲取token函數(shù)
url = "http://ip:port/v1.0/interfaceUrl"
data = {'userName': '****', 'password': '****'}
r = requests.post(url, data=data) #發(fā)送post請(qǐng)求
return (r.json()["data"]["token"]) # 將獲取的token返回4、獲取token的URL從配置文件中獲取
代碼實(shí)現(xiàn)
import requests
from getUrl import get_url
# 獲取token
def getToken():
#獲取token的URL
tokenUrl = get_url("token")
data = {'userName': '*****', 'password': '*****'}
r = requests.post(tokenUrl,data=data) #發(fā)送post請(qǐng)求
return (r.json()["data"]["token"]) # 將獲取的token返回總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python?pyecharts?Map地圖數(shù)據(jù)不顯示的原因及完美解決
這篇文章主要給大家介紹了關(guān)于Python?pyecharts?Map地圖數(shù)據(jù)不顯示的原因及解決辦法,pyecharts是一款將python與echarts結(jié)合的強(qiáng)大的數(shù)據(jù)可視化工具,文中通過圖文以及代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Python使用Tkinter實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)器的步驟詳解
這篇文章主要介紹了Python使用Tkinter實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)器,,本文分場(chǎng)景通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Pytorch如何加載自己的數(shù)據(jù)集(使用DataLoader讀取Dataset)
這篇文章主要介紹了Pytorch如何加載自己的數(shù)據(jù)集(使用DataLoader讀取Dataset)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
使用Keras實(shí)現(xiàn)簡(jiǎn)單線性回歸模型操作
這篇文章主要介紹了使用Keras實(shí)現(xiàn)簡(jiǎn)單線性回歸模型操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
python 應(yīng)用之Pycharm 新建模板默認(rèn)添加編碼格式-作者-時(shí)間等信息【推薦】
這篇文章主要介紹了Pycharm 新建模板默認(rèn)添加編碼格式-作者-時(shí)間等信息 ,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06

