def send_captcha_requests(session, urlmapping_obj, params=None, data=None, **kwargs): """ xml data example: <HashMap> <result_message>验证码校验失败,信息为空</result_message> <result_code>8</result_code> </HashMap> format result data. """ session.headers.update(urlmapping_obj.headers) response = session.request(method=urlmapping_obj.method, url=urlmapping_obj.url, params=params, data=data, timeout=10, # allow_redirects=False, **kwargs) Log.d(urlmapping_obj.url) if response.status_code == requests.codes.ok: if 'xhtml+xml' in response.headers['Content-Type']: data = response.text root = ET.fromstring(data) message = root.find('result_message').text code = root.find('result_code').text return {"result_message": message, "result_code": code} elif 'json' in response.headers['Content-Type']: return response.json() else: Log.w(response.url) Log.w(response.status_code) raise ResponseError else: Log.w(response.url) Log.w(response.status_code) raise ResponseCodeError
def rk_create(self, im_string, timeout=60): """ im: 图片字节 im_type: 题目类型 """ params = { 'typeid': 6113, 'timeout': timeout, } params.update(self.base_params) files = {'image': ('a.jpg', im_string)} while True: try: r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers) # Log.v("使用若快进行验证码识别") data = r.json() break except requests.RequestException: Log.w("提交若快打码请求出现问题, 正在重试中...") continue Log.d(data) return data
def check(self, results): params_data = {'answer': results, 'rand': 'sjrand', 'login_site': 'E'} json_response = send_captcha_requests( LOGIN_SESSION, LOGIN_URL_MAPPING["normal"]["captchaCheck"], params=params_data) Log.d('验证码校验结果: %s' % json_response) return json_status(json_response, [], ok_code=self.success_code)
def _uamtk(self): json_data = send_requests(LOGIN_SESSION, self.URLS["uamtk"], data={'appid': 'otn'}) Log.d(json_data) result, msg = json_status(json_data, ["result_message", "newapptk"]) if not result: return result, msg, None else: return result, msg, json_data["newapptk"]
def send_requests(session, urlmapping_obj, params=None, data=None, **kwargs): session.headers.update(urlmapping_obj.headers) if urlmapping_obj.method.lower() == 'post': session.headers.update({ "Content-Type": r'application/x-www-form-urlencoded; charset=UTF-8' }) else: session.headers.pop("Content-Type", None) try: Log.d("请求 url {url}".format(url=urlmapping_obj.url)) try: response = session.request( method=urlmapping_obj.method, url=urlmapping_obj.url, params=params, data=data, timeout=10, # allow_redirects=False, **kwargs) except requests.RequestException as e: Log.w(e) Log.w("请求{0}异常 ".format(urlmapping_obj.url)) raise ResponseError if params: Log.d("{url} Get 参数 {data}".format(url=urlmapping_obj.url, data=params)) if data: Log.d("{url} Post 参数 {data}".format(url=urlmapping_obj.url, data=data)) Log.d("返回response url {url}".format(url=response.url)) if response.status_code == requests.codes.ok: if 'xhtml+xml' in response.headers['Content-Type']: data = response.text root = ET.fromstring(data) result = {v.tag: v.text for v in root.getchildren()} return result if 'json' in response.headers['Content-Type']: result = response.json() Log.d("{url} 返回值 {result}".format(url=urlmapping_obj.url, result=result)) return result # other type return response.text else: Log.w(response.url) Log.w(response.status_code) Log.w("返回状态码有问题") except Exception as e: Log.e(e) return None
def rk_create(self, im_string, timeout=60): """ im: 图片字节 im_type: 题目类型 """ params = { 'typeid': 6113, 'timeout': timeout, } params.update(self.base_params) files = {'image': ('a.jpg', im_string)} r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers) # Log.v("使用若快进行验证码识别") data = r.json() Log.d(data) return data