Commit 89d53828ed276976fc05b16ad4c115cad9a42632
1 parent
165ae2bf
MySession.py新增需要填写method进行请求的方法
MySession.py维护了下网页端登录方法 MySession.py维护了下客户端端登录方法
Showing
2 changed files
with
53 additions
and
21 deletions
commons/MySession.py
... | ... | @@ -67,9 +67,12 @@ class mysession(): |
67 | 67 | # 则在再次使用该session对象求求该网站的其他网页都会默认使用该session之前使用的cookie等参数 |
68 | 68 | self.se = requests.session() |
69 | 69 | #更新cookie加入UAP_firmId |
70 | - firm=account.split("_")[0] | |
71 | 70 | co = requests.cookies.RequestsCookieJar() |
71 | + #backup:通过提前配置的方法更新市场id | |
72 | + firm=account.split("_")[0] | |
72 | 73 | co.set("UAP_firmId", self.firmid[firm]) |
74 | + #通过客户端登录的方法更新市场id | |
75 | + # co.set("UAP_firmId", str(self.get_login_info(account).json()["data"]["user"]["firmId"])) | |
73 | 76 | self.se.cookies.update(co) |
74 | 77 | # 使用session对象的方法POST/GET等 |
75 | 78 | re = self.se.post(url=self.url, headers=self.header, data=self.body, proxies=self.proxies, **kwargs) |
... | ... | @@ -81,24 +84,42 @@ class mysession(): |
81 | 84 | # 返回session对象,供其他接口使用 |
82 | 85 | return self.se |
83 | 86 | |
84 | - def get_session_client(self, account, **kwargs): | |
85 | - "get_session和get_session_client的方法只能用一个" | |
86 | - "如下代码,可以通过配置文件来控制登录的账户session" | |
87 | + def get_login_info(self, account, **kwargs): | |
88 | + "用于获取用户信息" | |
87 | 89 | self.body_client.update({"userName":com.get_global_config("global_data", "account", account).split("&")[0]}) |
88 | 90 | self.body_client.update({"password":pwdCry(com.get_global_config("global_data", "account", account).split("&")[1])}) |
89 | 91 | # requests.session()会话保持,比如使用session成功的登录了某个网站, |
90 | 92 | # 则在再次使用该session对象求求该网站的其他网页都会默认使用该session之前使用的cookie等参数 |
93 | + tmp = requests.post(url=self.url_client, headers=self.header_client, json=self.body_client, proxies=self.proxies, **kwargs) | |
94 | + # 使用session对象的方法POST/GET等 | |
95 | + #获取关键信息供其他接口header使用 | |
96 | + # self.UAP_accessToken=tmp.json()["data"]["accessToken"] | |
97 | + # self.UAP_refreshToken=tmp.json()["data"]["refreshToken"] | |
98 | + # 返回session对象,供其他接口使用 | |
99 | + return tmp | |
100 | + | |
101 | + def get_session_client(self, account, **kwargs): | |
102 | + "get_session和get_session_client的方法只能用一个" | |
103 | + self.body_client.update({"userName": com.get_global_config("global_data", "account", account).split("&")[0]}) | |
104 | + self.body_client.update( {"password": pwdCry(com.get_global_config("global_data", "account", account).split("&")[1])}) | |
105 | + # requests.session()会话保持,比如使用session成功的登录了某个网站, | |
106 | + # 则在再次使用该session对象求求该网站的其他网页都会默认使用该session之前使用的cookie等参数 | |
91 | 107 | self.se = requests.session() |
92 | 108 | # 使用session对象的方法POST/GET等 |
93 | - re = self.se.post(url=self.url_client, headers=self.header_client, json=self.body_client, proxies=self.proxies, **kwargs) | |
109 | + self.re = self.se.post(url=self.url_client, headers=self.header_client, json=self.body_client, proxies=self.proxies, **kwargs) | |
110 | + #组装请求的cookie | |
111 | + co = requests.cookies.RequestsCookieJar() | |
112 | + co.set("UAP_firmId", str(self.re.json()["data"]["user"]["firmId"])) | |
113 | + co.set("UAP_accessToken", self.re.json()["data"]["accessToken"]) | |
114 | + co.set("UAP_refreshToken", self.re.json()["data"]["refreshToken"]) | |
115 | + self.se.cookies.update(co) | |
94 | 116 | #获取关键信息供其他接口header使用 |
95 | - self.UAP_accessToken=re.json()["data"]["accessToken"] | |
96 | - self.UAP_refreshToken=re.json()["data"]["refreshToken"] | |
97 | - print(self.UAP_accessToken) | |
98 | - print(self.UAP_refreshToken) | |
117 | + self.UAP_accessToken=self.se.cookies["UAP_accessToken"] | |
118 | + self.UAP_refreshToken=self.se.cookies["UAP_refreshToken"] | |
99 | 119 | # 返回session对象,供其他接口使用 |
100 | 120 | return self.se |
101 | 121 | |
122 | + | |
102 | 123 | def close_session(self): |
103 | 124 | "关闭session" |
104 | 125 | self.se.close() |
... | ... | @@ -140,6 +161,19 @@ class mysession(): |
140 | 161 | res = requests.request(method=method, url=url, data=data, headers=self.webHeaders, **kwargs) |
141 | 162 | return res |
142 | 163 | |
164 | + | |
165 | + def request(self, method ,url, **kwargs): | |
166 | + """:param method: method for the new :class:`Request` object: | |
167 | + ``GET``, ``OPTIONS``, ``HEAD``, ``POST``, ``PUT``, ``PATCH``, or ``DELETE``. | |
168 | + """ | |
169 | + # 记录日志 | |
170 | + log.info("{0:=^86}".format('')) | |
171 | + log.info("{}\n{}\n".format(url, kwargs)) | |
172 | + # 进行请求 | |
173 | + re = self.se.request(method , url, **kwargs, proxies=self.proxies, timeout=self.timeout) | |
174 | + return re | |
175 | + | |
176 | + | |
143 | 177 | def get(self, url, **kwargs): |
144 | 178 | """Sends a GET request. Returns :class:`Response` object. |
145 | 179 | |
... | ... | @@ -252,10 +286,9 @@ class mysession(): |
252 | 286 | my = mysession() |
253 | 287 | my.set_mark() |
254 | 288 | my.cliLogin() |
255 | -s1 = my.get_session("sg_user01") | |
256 | -# s2 = my.get_session("sg_user02") | |
257 | -# print(s1.cookies) | |
258 | -# s3 = my.get_session_client("sg_user02") | |
259 | -# print(s3.cookies) | |
260 | -# sg1=my.get_session("sg_user01") | |
261 | -# sy1=my.get_session("sy_user01") | |
289 | +s1 = my.get_session_client("sg_user01") | |
290 | +s2 = my.get_session_client("sg_user02") | |
291 | +# s3 = my.get_session("sg_user01") | |
292 | +# s3= my.get_session_client("sg_user02") | |
293 | +# sg=my.get_session_client("sg_user01") | |
294 | +# sy=my.get_session_client("sy_user01") | |
262 | 295 | \ No newline at end of file | ... | ... |
report/test.log
1 | -[2021-07-22 15:46:06] [INFO] : ====================================================================================== | |
2 | -[2021-07-22 15:46:06] [INFO] : http://test.card.diligrp.com:8386/accountQuery/page.action | |
3 | -rows=10&page=1&sort=card_create_time&order=desc | |
4 | -None | |
5 | -{'headers': {'Host': 'test.card.diligrp.com:8386', 'Connection': 'keep-alive', 'Content-Length': '47', 'Accept': 'application/json,text/javascript,*/*;q=0.01', 'Origin': 'http://test.card.diligrp.com:8386', 'X-Requested-With': 'XMLHttpRequest', 'User-Agent': 'Mozilla/5.0(WindowsNT6.2;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/84.0.4147.105Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Referer': 'http://test.card.diligrp.com:8386/accountQuery/list.html', 'Accept-Encoding': 'gzip,deflate'}} | |
1 | +[2021-07-22 17:11:13] [INFO] : ====================================================================================== | |
2 | +[2021-07-22 17:11:13] [INFO] : http://test.card.diligrp.com:8386/accountQuery/page.action | |
3 | +{'headers': {'Host': 'test.card.diligrp.com:8386', 'Connection': 'keep-alive', 'Content-Length': '47', 'Accept': 'application/json,text/javascript,*/*;q=0.01', 'Origin': 'http://test.card.diligrp.com:8386', 'X-Requested-With': 'XMLHttpRequest', 'User-Agent': 'Mozilla/5.0(WindowsNT6.2;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/84.0.4147.105Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Referer': 'http://test.card.diligrp.com:8386/accountQuery/list.html', 'Accept-Encoding': 'gzip,deflate'}, 'data': 'rows=10&page=1&sort=card_create_time&order=desc'} | |
4 | + | ... | ... |