def __send_request(self, sign_mode, method, url, params=None, data=None, datastr=None, json=None, **kwargs): ret_json = None # if self.__is_login: # self._login() headers = self.__get_request_header(data=data) if 'headers' in kwargs: headers.update(kwargs.pop('headers')) retry_for_sign_mode = True # if 'sign' in params or 'sign' in str(data): # retry_for_sign_mode = False if params and 'sign' in params: retry_for_sign_mode = False if data and 'sign' in data: retry_for_sign_mode = False if datastr and 'sign' in datastr: retry_for_sign_mode = False if json and 'sign' in json: retry_for_sign_mode = False # 尝试不同的加签模式进行签名并发送请求,遇到签名验证通过的结果即返回 if sign_mode: params_prepared = self.__append_request_params(origin_params=params, sign_mode=sign_mode) if 'get' == method.lower() else self.__append_request_params( origin_params=datastr if datastr else data, sign_mode=sign_mode) if 'get' == method.lower(): response = self.request_session.get(url, params=params_prepared, headers=headers, verify=False, **kwargs) elif datastr: response = self.request_session.post(url, data=params_prepared, headers=headers, verify=False, **kwargs) else: response = self.request_session.post(url, json=params_prepared, headers=headers, verify=False, **kwargs) ret_json = helper.check_json_response(response) elif retry_for_sign_mode: for sm in range(0, 12): params_prepared = self.__append_request_params(origin_params=params, sign_mode=sm) if 'get' == method.lower() else self.__append_request_params( origin_params=datastr if datastr else data, sign_mode=sm) if 'get' == method.lower(): response = self.request_session.get(url, params=params_prepared, headers=headers, verify=False, **kwargs) elif datastr: response = self.request_session.post(url, data=params_prepared, headers=headers, verify=False, **kwargs) else: response = self.request_session.post(url, json=params_prepared, headers=headers, verify=False, **kwargs) ret_json = helper.check_json_response(response) # 如果返回的结果是签名验证失败(703),则继续循环尝试采用不同的签名方式进行验证,否则退出循环 if 'code' in ret_json and (ret_json['code'] == 703): continue else: break else: if 'get' == method.lower(): response = self.request_session.get(url, params=params, headers=headers, verify=False, **kwargs) else: if isinstance(data, dict): response = self.request_session.post(url, json=data, headers=headers, verify=False, **kwargs) else: response = self.request_session.post(url, data=data, json=json, headers=headers, verify=False, **kwargs) ret_json = helper.check_json_response(response) return ret_json
def auth_visitor_login(): login_session = requests.session() timestamp = int(time.time() * 1000) data = { "platform": 3, "deviceToken": "B5E1B1E4-BAA1-4F06-9011-A1E21A99DA79", "deviceName": "iPhone 6s", "sign": helper.get_str_md5(helper.APP_AES_KEY + "B5E1B1E4-BAA1-4F06-9011-A1E21A99DA79" + str(timestamp) + helper.APP_AES_KEY), "timestamp": timestamp } json_data = json.dumps(data) response = login_session.post(urls.auth_visitor_login, data=json_data, headers={"Content-Type": "application/json"}) if response.status_code != 200: print("uc登录异常: {}".format(response.text)) ret_json = helper.check_json_response(response) login_session.close() if int(ret_json['code']) != 200: print("uc登录异常:{}".format(ret_json)) return ret_json
def __get_oatoken(self, schoolid): data = { "uid": 1234, "schoolId": schoolid, "userName": "******", "userRole": 2, "projectId": 12345, "expireYear": 2022 } res = helper.check_json_response(requests.SessionV2().post(url=TeacherUrls().oa_auth, json=data)) return res['data']['oaToken']
def post(self, url, data=None, **kwargs): # if self.__is_login: # self._login() headers = {} if 'headers' in kwargs: headers.update(kwargs.pop('headers')) if isinstance(data, dict): data = json.dumps(data) headers["Content-Type"] = "application/json" response = self.request_session.post(url, data=data, headers=headers, verify=False, **kwargs) ret_json = helper.check_json_response(response) return ret_json
def get(self, url, params=None, **kwargs): # if self.__is_login: # self._login() headers = {} if 'headers' in kwargs: headers.update(kwargs.pop('headers')) # bend项目全局get请求参数加了时间戳 if 'bendbff' in url and params: params['timestamp'] = int(time.time()*1000) response = self.request_session.get(url, params=params, headers=headers, verify=False, **kwargs) ret_json = helper.check_json_response(response) return ret_json
def __send_request(self, method, url, params=None, data=None, json=None, **kwargs): headers = self.__get_request_header(data=data) if 'headers' in kwargs: headers.update(kwargs.pop('headers')) if 'get' == method.lower(): response = self.request_session.get(url, params=params, headers=headers, verify=False, **kwargs) else: if isinstance(data, dict): response = self.request_session.post(url, json=data, headers=headers, verify=False, **kwargs) else: response = self.request_session.post(url, data=data, json=json, headers=headers, verify=False, **kwargs) ret_json = helper.check_json_response(response) return ret_json
def auth_pc_login(username, password='******'): login_session = requests.session() encrypt_password = helper.encrypt_pc_password(password) data = {"userName": username, "password": encrypt_password, "platform": 1} json_data = json.dumps(data) response = login_session.post(urls.auth_pc_login, data=json_data, headers={"Content-Type": "application/json"}) if response.status_code != 200: print("uc登录异常: {}".format(response.text)) ret_json = helper.check_json_response(response) login_session.close() if int(ret_json['code']) != 200: print("uc登录异常:{}".format(ret_json)) return ret_json
def app_login(username, password='******'): login_session = requests.session() encrypt_password = helper.encrypt_app_password(password) data = "password={0}&sign=10d20dd481a694bf73bbd75aa41062e9" \ "&devicename=iPhone 6s&devicetoken=B5E1B1E4-BAA1-4F06-9011-A1E21A99DA79" \ "&platform=2×tamp={2}&username={1}&" \ "token=&sid=7".format(encrypt_password, username, int(time.time() * 1000)) response = login_session.post( urls.app_login, data=data, headers={"Content-Type": "application/x-www-form-urlencoded"}, verify=False) if response.status_code != 200: print("uc登录异常: {}".format(response.text)) ret_json = helper.check_json_response(response) login_session.close() if int(ret_json['code']) != 200: print("uc登录异常: {}".format(ret_json)) return ret_json['data']['member']['token']
def auth_app_login(username='******', password='******'): login_session = requests.session() encrypt_password = helper.encrypt_app_password(password) # platform: 1:WEB,2:ANDROID,3:IOS data = { "userName": username, "password": encrypt_password, "platform": 3, "deviceToken": "B5E1B1E4-BAA1-4F06-9011-A1E21A99DA79", "deviceName": "iPhone 6s" } json_data = json.dumps(data) response = login_session.post(urls.auth_app_login, data=json_data, headers={"Content-Type": "application/json"}) if response.status_code != 200: print("uc登录异常: {}".format(response.text)) ret_json1 = helper.check_json_response(response) login_session.close() if int(ret_json1['code']) != 200: print("uc登录异常: {}".format(ret_json1)) return ret_json1, ret_json1['data']['token']
def post(self, url, data=None, json=None, **kwargs): headers = self.__get_request_header() if 'headers' in kwargs: headers.update(kwargs.pop('headers')) response = self.request_session.post(url=url, headers=headers, verify=False, data=data, json=json, **kwargs) return helper.check_json_response(response)
def get(self, url, params=None, **kwargs): response = self.request_session.get(url=url, headers=self.__get_request_header(), params=params, verify=False, **kwargs) return helper.check_json_response(response)