pycookiecheat库是一个用于读取和导出浏览器cookie的实用工具。它可以帮助你访问浏览器中存储的cookie数据,并在需要时将其用于进行HTTP请求。该库支持Chrome和Firefox浏览器。安装命令如下:
pip/pip3 install pycookiecheat
使用示例:使用pycookiecheat获取chrome浏览器nps平台和cmdb平台的cookie数据,然后进行http请求查询平台数据:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @FileName: nps.py @Function: operating nps platform @Author: Zhihe An @Contact: anzhihe@chegva.com @Time: 2023/6/8 """ import requests import json import sys import platform import prettytable as pt from pycookiecheat import chrome_cookies import argparse class ServiceInterface: """调用平台接口""" def __init__(self, vehicle_number = None): self.vehicle_number = vehicle_number def get_nps_info(self, url = None): """查询车辆nps信息""" nps_cookies = chrome_cookies('http://nps.chegva.com') #print(nps_cookies) # cookies = { # 'access_token': 'anzhihe_89e50344a07442111171c4a5f3', # 'jwt_token': 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxMDM5LCJ1YW1fdG9rZW4iOiJhbnpoaWhlXzg5ZTUwMzQ0YTA3NDQyNDM5YTFkZGNlYjcxYzRhNWYzIiwidXNlcm5hbWUiOiJhbnpoaWhlIn0.M17TZYaxMLFhemNuUBzQGI4MLydAN1wMNwhDhhBAiqb5cbZSpFKlIqbpcaep0QNn3nph9t14EkEVoETrloqf1g', # 'lang': 'zh-CN', # 'beegosessionID': 'b2a2a4feaa58fa38bed4a43aa9aa4a3d', # } headers = { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Origin': 'http://nps.chegva.com:10086', 'Pragma': 'no-cache', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36', } data = { 'search': self.vehicle_number, 'order': 'asc', 'offset': '0', 'limit': '10', } resp_vehicle = requests.post('http://nps.chegva.com:10086/client/list', cookies=nps_cookies, headers=headers, data=data, verify=False) vehicle_info = json.loads(resp_vehicle.text) yk_table_header = ['ID', '域控', '验证密钥', '连接', '端口映射', 'Mac'] yk_table = make_table(yk_table_header, align='c', width=2) for veh in vehicle_info["rows"]: data_yk = { 'offset': '0', 'limit': '10', 'type': '', 'client_id': veh["Id"], 'search': '', } resp_yk = requests.post('http://nps.chegva.com:10086/index/gettunnel', cookies=nps_cookies, headers=headers, data=data_yk, verify=False) yk_info = json.loads(resp_yk.text) macs = [] port_maps = [] for yk in yk_info["rows"]: port_mapping = str(yk["Port"]) + ":" + yk["Target"]["TargetStr"] port_maps.append(port_mapping) if yk["Mac"]: macs.append(yk["Mac"]) online_status = "在线" if veh["IsConnect"] else "离线" yk_table.add_row([veh["Id"], veh["Remark"], veh["VerifyKey"], online_status, "\n".join('%s' %port for port in port_maps), "\n".join('%s' %mac for mac in macs)]) # divider=True 给每行添加分隔线 print(yk_table) # cmdb查询信息 cmdb_cookies = chrome_cookies('https://cmdb.chegva.com/api/data/list') # print(cmdb_cookies) cmdb_query_data = { 'params': { 'pageNum': 1, 'pageSize': 10, }, 'modelId': 1, 'searchKey': 'number_plate', 'searchVal': self.vehicle_number, } cmdb_headers = { 'authority': 'cmdb.chegva.com', 'accept': 'application/json, text/plain, */*', 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7', 'cache-control': 'no-cache', 'origin': 'https://cmdb.chegva.com', 'pragma': 'no-cache', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36', } resp_cmdb_vehicle = requests.post('https://cmdb.chegva.com/api/data/list', cookies=cmdb_cookies, headers=cmdb_headers, json=cmdb_query_data) cmdb_vehicle_info = json.loads(resp_cmdb_vehicle.text) ...... def make_table(header, align=None, width=None): """Make a table object for output.""" encoding = "UTF-8" if platform.system().lower() in ('linux', 'darwin') else "GBK" table = pt.PrettyTable(encoding=encoding, field_names=header, align=align) table.padding_width = width return table def parse_args(argv): p = argparse.ArgumentParser(description="Call Service Platform Interfaces. <author: anzhihe@chegva.com>") p.add_argument("--vehicle_number", "-nps", metavar="[veh1,veh2,veh3,...]", help="Input vehicle number to query nps information,separate with ','", type=str, action="store") if len(sys.argv) == 1: p.print_help() sys.exit(1) args = p.parse_args(argv) return args def main(): options = parse_args(sys.argv[1:]) if options.vehicle_number: ServiceInterface(options.vehicle_number).get_nps_info() if __name__ == '__main__': main()
在Mac终端上加个函数方便调用脚本:
cat ~/.bash_aliases 01:52:31 function nps() { if [[ "$1" == "-h" || "$1" == "--help" || -z "$1" ]]; then arch -arm64 python3 ~/anzhihe/op/scripts/python/nps.py -h 2>/dev/null else arch -arm64 python3 ~/anzhihe/op/scripts/python/nps.py -nps $1 2>/dev/null fi }