def get_data(content): print 'start filter html content' content = smart_decode(content) try: doc = pq(etree.fromstring(content)) except: doc = pq(content) screen_view = doc('.screen-view') page_data = {'game_in_progress': [], 'game_finished': []} print 'filter current game data' for tr in screen_view.find('tr'): simple_game_data = [] for td in pq(tr).find('td'): name = pq(td)('a').filter('.color-666') score = pq(td)('b') if name: team_name = HTMLParser().unescape(name.text()), simple_game_data.append(team_name[0].strip() if team_name else '') if score: current_score = HTMLParser().unescape(score.text()), simple_game_data.append(current_score[0].strip() if current_score else '') if len(simple_game_data) == 3: page_data['game_in_progress'].append({u'team1': simple_game_data[0], u'current_score': simple_game_data[1], u'team2': simple_game_data[2]}) elif len(simple_game_data) == 2: page_data['game_finished'].append({u'team1': simple_game_data[0], u'team2': simple_game_data[1]}) # print json.dumps(page_data, ensure_ascii=False) print 'return page data , request has been dealed!' return page_data
def aes_decode(data): ''' aes解密 :param ciphertext: 密文 :param key: 用来解密的key,和加密的key是同一个,所以是对称加密 :return: ''' file_path = get_path(u'key.bcp') with open(file_path) as file: key = file.read() data = smart_decode(data) key = smart_decode(key).encode('utf8') # 解密的话要用key和iv生成新的AES对象 mydecrypt = AES.new(key, AES.MODE_CFB, data[:16]) # 使用新生成的AES对象,将加密的密文解密 decrypttext = mydecrypt.decrypt(data[16:]) return decrypttext.decode('utf8')
def handle(self): source_data = self.rfile.readline().strip() encode_type = source_data[:7] secret_data = source_data[7:] secret_data = smart_decode(secret_data) if encode_type.upper().startswith('AES'): print aes_decode(secret_data) elif encode_type.upper().startswith('RSA'): print rsa_decode(secret_data) else: print u'客户端传入数据不合法,请检查代码'
def rsa_encode(text): ''' 加密函数,使用公钥加密 :param text: 明文 :return: ''' public_key = read_key('public.pem') public_key = rsa.PublicKey.load_pkcs1(public_key) text = smart_decode(text).encode('utf8') crypto = rsa.encrypt(text, public_key) return crypto
def aes_encode(data, key): ''' aes加密 :param data: 明文 :param key: 用来加密的key,和解密的key是同一个,所以是对称加密 :return: ''' if len(key) not in [16, 24, 32]: print u'秘钥必须为16, 24, 32位' raise Exception data = smart_decode(data) key = smart_decode(key).encode('utf8') # 生成长度等于AES块大小的不可重复的密钥向量 iv = Random.new().read(AES.block_size) # 使用key和iv初始化AES对象, 使用MODE_CFB模式 mycipher = AES.new(key, AES.MODE_CFB, iv) # 加密的明文长度必须为16的倍数,如果长度不为16的倍数,则需要补足为16的倍数 # 将iv(密钥向量)加到加密的密文开头,一起传输 ciphertext = iv + mycipher.encrypt(data.encode('utf8')) return ciphertext
def get_dom_html(): dom_html = '' print('set options') chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--disable-extensions') chrome_options.add_argument('blink-settings=imagesEnabled=false') print('set driver') thor_main_dir = os.path.dirname(os.path.dirname(__file__)) if platform.system().lower() == 'windows': driver_path = os.path.join(thor_main_dir, 'public', 'chromedriver.exe') else: driver_path = os.path.join(thor_main_dir, 'public', 'chromedriver') print('driver path is: %s' % driver_path) driver = webdriver.Chrome(executable_path=driver_path, chrome_options=chrome_options) driver.maximize_window() try: print('send get request') base_url = "https://free.leisu.com/" driver.get(base_url) datetime_string = datetime.datetime.now().strftime('%Y%m%d%H%M%S') print('waiting for dom render...') table_dom = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CLASS_NAME, "screen-view"))) print('waiting for another 5 seconds...') time.sleep(5) print('create html file') file_path = os.path.join(thor_main_dir, 'data', '%s.html' % datetime_string) print('create html file: %s' % file_path) dom_html = smart_decode( table_dom.get_attribute('outerHTML')).encode('utf8') with open(file_path, 'w') as file: file.write(dom_html) # print('save page png') # png_path = os.path.join(os.path.dirname(os.getcwd()), # 'data', # 'img', # '%s.png' % datetime_string) # driver.save_screenshot(png_path) except Exception, e: print('*' * 25 + 'Error' + '*' * 25) print(traceback.format_exc())
secret_data = None encode_type = None while True: user_input = get_user_input( u'请选择模式传输内容的模式(输入序号)\n 1、手动输入字符\n 2、输入文件路径读取内容', [1, 2]) if user_input == '1': source_data = get_user_input(u'请输入需要加密传输的内容') else: file_path = get_user_input(remind=u'请输入文件绝对路径,请注意输入可读文件!', validate_fun=file_validate) with open(file_path, 'r') as file: source_data = file.read() user_input = get_user_input( u'待发送的数据如下请确认(输入序号)\n %s \n 1、确认 \n 2、重新选择' % smart_decode(source_data), [1, 2]) if user_input == '2': continue encode_type = get_user_input(u'请选择加密模式(输入序号)\n 1、AES-256\n 2、RSA-256', [1, 2, 3, 4]) ascii_byte = str(int(encode_type) * 8 + 8) encode_type_map = {'1': 'AES-256', '2': 'RSA-256'} encode_type = encode_type_map.get(encode_type) if encode_type in ['AES-256']: key = read_key(u'key.bcp') secret_data = aes_encode(source_data, key) elif encode_type in ['RSA-256']: secret_data = rsa_encode(source_data) else: print u'选择错误,请重新执行。。。'