Commit e3f10192f0322ffe134f1da465f5bd4992f4ab10
1 parent
bb70b111
MySession.py新增客户端登录方法get_session_client
Showing
2 changed files
with
40 additions
and
16 deletions
commons/MySession.py
... | ... | @@ -5,12 +5,15 @@ import json |
5 | 5 | from commons import common as com |
6 | 6 | from commons.Logging import Logger |
7 | 7 | from commons.clientSession import cliSession |
8 | +from commons.scripts.pwdCry import pwdCry | |
8 | 9 | log=Logger() |
9 | 10 | |
10 | 11 | class mysession(): |
11 | 12 | "封装了requests的基类,以供后期统一使用" |
12 | 13 | |
13 | 14 | url = "http://test.uap.diligrp.com/login/login.action" |
15 | + url_client = "http://test.uap.diligrp.com/api/authenticationApi/loginWeb" | |
16 | + | |
14 | 17 | header = { |
15 | 18 | "Host": "test.uap.diligrp.com", |
16 | 19 | "Connection": "keep-alive", |
... | ... | @@ -25,19 +28,29 @@ class mysession(): |
25 | 28 | "Accept-Encoding": "gzip,deflate", |
26 | 29 | "Accept-Language": "zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7", |
27 | 30 | "Cookie": "UAP_accessToken=;UAP_refreshToken=;UAP_loginPath="} |
31 | + header_client = { | |
32 | + "Content-Type": "text/plain;charset\u003dutf-8", | |
33 | + "Host": "test.uap.diligrp.com", | |
34 | + "Content-Length": "209", | |
35 | + "Expect": "100-continue"} | |
36 | + | |
28 | 37 | body = "userName=sg_wenze&password=111111" |
38 | + body_client= {"userName":"sg_wenze","password":"key"} | |
29 | 39 | |
30 | 40 | def __init__(self): |
31 | 41 | "如下代码,可以通过配置文件来控制测试环境和灰度环境,http和https" |
32 | 42 | self.url = mysession.url.replace("http://test.", com.get_global_config("global_data", "environment", "en")) |
33 | 43 | self.header = mysession.header |
34 | 44 | self.body = mysession.body |
45 | + self.url_client = mysession.url_client.replace("http://test.", com.get_global_config("global_data", "environment", "en")) | |
46 | + self.header_client = mysession.header_client | |
47 | + self.body_client = mysession.body_client | |
35 | 48 | self.timeout = (5, 5) |
36 | 49 | self.max_retries = 3 |
37 | 50 | self.keep_alive = False |
38 | 51 | self.ssl_verify = False |
39 | - self.proxies = None | |
40 | -# self.proxies={'http': 'http://localhost:8888', 'https': 'http://localhost:8888'} | |
52 | + # self.proxies = None | |
53 | + self.proxies={'http': 'http://localhost:8888', 'https': 'http://localhost:8888'} | |
41 | 54 | self.allow_redirects = False |
42 | 55 | self.firmid={"group":"1","hd":"2","cd":"3","qqhe":"4","mdj":"5","gy":"6","cc":"7","sg":"8","sy":"9"} |
43 | 56 | |
... | ... | @@ -46,11 +59,11 @@ class mysession(): |
46 | 59 | self.webHeaders, self.clientHeaders, self.userInfo = cliSession().loginUser() |
47 | 60 | |
48 | 61 | def get_session(self, account, **kwargs): |
49 | - # print("get_session") | |
50 | 62 | "如下代码,可以通过配置文件来控制登录的账户session" |
51 | 63 | self.body = self.body.replace("sg_wenze", |
52 | 64 | com.get_global_config("global_data", "account", account).split("&")[0]) |
53 | - self.body = self.body.replace("111111", com.get_global_config("global_data", "account", account).split("&")[1]) | |
65 | + self.body = self.body.replace("111111", | |
66 | + com.get_global_config("global_data", "account", account).split("&")[1]) | |
54 | 67 | # requests.session()会话保持,比如使用session成功的登录了某个网站, |
55 | 68 | # 则在再次使用该session对象求求该网站的其他网页都会默认使用该session之前使用的cookie等参数 |
56 | 69 | self.se = requests.session() |
... | ... | @@ -64,9 +77,30 @@ class mysession(): |
64 | 77 | #获取关键信息供其他接口header使用 |
65 | 78 | self.UAP_accessToken=self.se.cookies["UAP_accessToken"] |
66 | 79 | self.UAP_refreshToken=self.se.cookies["UAP_refreshToken"] |
80 | + print(self.UAP_accessToken) | |
81 | + print(self.UAP_refreshToken) | |
67 | 82 | # 返回session对象,供其他接口使用 |
68 | 83 | return self.se |
69 | 84 | |
85 | + def get_session_client(self, account, **kwargs): | |
86 | + "get_session和get_session_client的方法只能用一个" | |
87 | + "如下代码,可以通过配置文件来控制登录的账户session" | |
88 | + self.body_client.update({"userName":com.get_global_config("global_data", "account", account).split("&")[0]}) | |
89 | + self.body_client.update({"password":pwdCry(com.get_global_config("global_data", "account", account).split("&")[1])}) | |
90 | + # requests.session()会话保持,比如使用session成功的登录了某个网站, | |
91 | + # 则在再次使用该session对象求求该网站的其他网页都会默认使用该session之前使用的cookie等参数 | |
92 | + self.se = requests.session() | |
93 | + # 使用session对象的方法POST/GET等 | |
94 | + re = self.se.post(url=self.url_client, headers=self.header_client, json=self.body_client, proxies=self.proxies, **kwargs) | |
95 | + #获取关键信息供其他接口header使用 | |
96 | + self.UAP_accessToken=re.json()["data"]["accessToken"] | |
97 | + self.UAP_refreshToken=re.json()["data"]["refreshToken"] | |
98 | + print(self.UAP_accessToken) | |
99 | + print(self.UAP_refreshToken) | |
100 | + # 返回session对象,供其他接口使用 | |
101 | + return self.se | |
102 | + | |
103 | + | |
70 | 104 | def close_session(self): |
71 | 105 | "关闭session" |
72 | 106 | self.se.close() |
... | ... | @@ -222,5 +256,7 @@ my.set_mark() |
222 | 256 | my.cliLogin() |
223 | 257 | s1 = my.get_session("sg_user01") |
224 | 258 | s2 = my.get_session("sg_user02") |
259 | +s3 = my.get_session_client("sg_user01") | |
225 | 260 | # sg1=my.get_session("sg_user01") |
226 | 261 | # sy1=my.get_session("sy_user01") |
262 | + | ... | ... |
report/test.log
1 | -[2021-07-21 10:40:56] [INFO] : ====================================================================================== | |
2 | -[2021-07-21 10:40:56] [INFO] : http://test.trace.diligrp.com:8393/fieldConfig/doUpdate.action | |
3 | -None | |
4 | -{'moduleType': '1', 'marketId': '8', 'fieldConfigDetailList': [{'fieldName': 'unitPrice', 'jsonPath': '$.unitPrice', 'defaultId': '3', 'displayed': '1', 'required': '0'}, {'fieldName': 'arrivalTallynos', 'jsonPath': '$.arrivalTallynos', 'defaultId': '10', 'displayed': '1', 'required': '0'}, {'fieldName': 'brandName', 'jsonPath': '$.brandName', 'defaultId': '5', 'displayed': '1', 'required': '0'}, {'fieldName': 'truckTareWeight', 'jsonPath': '$.truckTareWeight', 'defaultId': '1', 'displayed': '1', 'required': '0'}, {'fieldName': 'originId', 'jsonPath': '$.originId', 'defaultId': '7', 'displayed': '1', 'required': '0'}, {'fieldName': 'specName', 'jsonPath': '$.specName', 'defaultId': '4', 'displayed': '1', 'required': '0'}, {'fieldName': 'truckType', 'jsonPath': '$.truckType', 'defaultId': '2', 'displayed': '1', 'required': '1', 'availableValueList': ['10', '20']}, {'fieldName': 'imageCertList', 'jsonPath': '$.imageCertList[*]certType', 'defaultId': '11', 'displayed': '1', 'required': '0', 'availableValueList': ['2', '3']}, {'fieldName': 'arrivalDatetime', 'jsonPath': '$.arrivalDatetime', 'defaultId': '9', 'displayed': '1', 'required': '0'}, {'fieldName': 'remark', 'jsonPath': '$.remark', 'defaultId': '8', 'displayed': '1', 'required': '0'}, {'fieldName': 'remark', 'jsonPath': '$.remark', 'defaultId': '6', 'displayed': '1', 'required': '0'}, {'fieldName': 'measureType', 'jsonPath': '$.measureType', 'defaultId': '12', 'displayed': '1', 'required': '1', 'availableValueList': ['10', '20']}]} | |
5 | -{'headers': {'Host': 'test.trace.diligrp.com:8393', 'Connection': 'keep-alive', 'Content-Length': '1378', 'Accept': 'application/json,text/javascript,*/*;q=0.01', 'X-Requested-With': 'XMLHttpRequest', 'User-Agent': 'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/91.0.4472.77Safari/537.36', 'Content-Type': 'application/json', 'Origin': 'http://test.trace.diligrp.com:8393', 'Referer': 'http://test.trace.diligrp.com:8393/fieldConfig/bill.html', 'Accept-Encoding': 'gzip,deflate', 'Accept-Language': 'zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7'}} | |
6 | -[2021-07-21 10:40:57] [INFO] : ====================================================================================== | |
7 | -[2021-07-21 10:40:57] [INFO] : http://test.trace.diligrp.com:8393/newRegisterBill/doAdd.action | |
8 | -{"registerHeadWeight": "0", "registerHeadRemainWeight": "0", "imageCertList": [], "measureType": 20, "registType": 10, "userId": 516, "arrivalTallynos": [], "arrivalDatetime": "", "truckType": 10, "weight": "111", "weightUnit": 1, "productName": "\u5927\u767d\u83dc", "productId": 2190, "originName": "", "originId": "", "unitPrice": "", "truckTareWeight": "", "remark": "", "specName": "", "brandName": "", "plate": "\u5dddA123456"} | |
9 | -{} | |
10 | - | |
11 | -[2021-07-21 10:40:59] [INFO] : 发送邮件 | |
12 | -[2021-07-21 10:41:03] [INFO] : 邮件发送完毕 |