def login(self): self.pre_login() login_params = { "function_id": 200, "login_type": "stock", "version": 200, "identity_type": "", "remember_me": "", "input_content": 1, "content_type": 0, "loginPasswordType": "B64", "disk_serial_id": self.disk_serial_id, "cpuid": self.cpuid, "machinecode": self.machinecode, "mac_addr": self.mac_address, "account_content": self._account, "password": urllib.parse.unquote(self._password), "validateCode": self.vcode } logger.debug('login_params is: %s' % login_params) r = self._session.post( 'https://jy.yongjinbao.com.cn/winner_gj/gjzq/exchange.action', params=login_params) r.raise_for_status() logger.debug('Login respone: %s' % r.text) returnJson = r.json()['returnJson'] data = demjson.decode(returnJson) error_msg = dict() if data['msg_no'] != '0': if 'msg_info' in data.keys() and data['msg_info'] != '': error_msg['error_info'] = data['msg_info'] else: error_msg = data[data['error_grids']][1] if error_msg['error_info'].find('验证码') != -1: logger.warning('vcode error : %s' % error_msg['error_info']) raise VerifyCodeError(error_msg['error_info']) else: logger.error('login Failed :%s' % error_msg['error_info']) raise LoginFailedError(error_msg['error_info']) return
def vcode(self): r = self._session.get( 'https://jy.yongjinbao.com.cn/winner_gj/gjzq/user/extraCode.jsp', params={'randomStamp': random.random()}) r.raise_for_status() # 通过内存保存数据 img_buffer = BytesIO(r.content) img = Image.open(img_buffer) code = pytesseract.image_to_string(img) img.close() img_buffer.close() if self.code_rule.findall(code) == []: raise VerifyCodeError('Wrong verify code: %s' % code) else: logger.debug('Verify Code is: %s' % code) return code
def login(self): # 无论是否登录,都重新创建一个session对象 # self.pre_login() login_params = { "authtype": 2, "disknum": self.disknum, "loginType": 2, "origin": "web", 'mac': self.mac_address, 'username': self._account, 'password': self._password, 'tmp_yzm': self.vcode } resq = self._session.post(url='https://trade.gf.com.cn/login', params=login_params) resq.raise_for_status() logger.debug('login resq: %s' % resq.json()) data = resq.json() if data['success'] == True: v = resq.headers self._dse_sessionId = v['Set-Cookie'][-32:] # 等待服务器准备就绪 time.sleep(0.1) logger.info('Login success: %s' % self._dse_sessionId) return elif data['success'] == False and 'error_info' not in data.keys(): logger.warning('当前系统无法登陆') raise TraderAPIError(data) elif data['error_info'].find('验证码') != -1: self.dse_sessionId = None logger.warning('VerifyCode Error: %s' % data) raise VerifyCodeError(data['error_info']) else: self.dse_sessionId = None logger.warning('API Login Error: %s' % data) raise TraderAPIError(data['error_info'])
def vcode(self): # 获取校验码 r = self._session.get('https://trade.gf.com.cn/yzm.jpgx') r.raise_for_status() # 通过内存保存图片,进行识别 img_buffer = BytesIO(r.content) img = Image.open(img_buffer) if hasattr(img, "width"): width, height = img.width, img.height else: width, height = img.size for x in range(width): for y in range(height): if img.getpixel((x, y)) < (100, 100, 100): img.putpixel((x, y), (256, 256, 256)) gray = img.convert('L') two = gray.point(lambda x: 0 if 68 < x < 90 else 256) min_res = two.filter(ImageFilter.MinFilter) med_res = min_res.filter(ImageFilter.MedianFilter) for _ in range(1): med_res = med_res.filter(ImageFilter.MedianFilter) # 通过tesseract-ocr的工具进行校验码识别 vcode = pytesseract.image_to_string(med_res) img.close() img_buffer.close() vcode = vcode.replace(' ', '') if self.code_rule.findall(vcode) != []: logger.debug('vcode is: %s' % vcode) return vcode else: raise VerifyCodeError('verify code error: %s' % vcode)