Merge remote-tracking branch 'origin/main'
# Conflicts: # test_websocket.py
This commit is contained in:
124
generateSign.py
Normal file
124
generateSign.py
Normal file
@@ -0,0 +1,124 @@
|
||||
import json
|
||||
import time
|
||||
import uuid
|
||||
import urllib.parse
|
||||
import hashlib
|
||||
import sys
|
||||
|
||||
|
||||
# =================== 运行环境配置 ===================
|
||||
CONFIG = {
|
||||
"dev": {
|
||||
"platform_ifc_system_code": "ef5b17caff6e4da19d6af82d539e894d",
|
||||
"systemCode": "57395d2bc668496c9c57d8f2b19bd516",
|
||||
"appId": "57395d2bc668496c9c57d8f2b19bd516",
|
||||
"secret_key": "KMFHKo1Uzrl&MWXorbQIT&C$Qea$uQOY",
|
||||
"ifcUrl": "http://192.168.1.202:8083/dev-api/cenertech-interface-center/IFC2"
|
||||
},
|
||||
"prod": {
|
||||
"platform_ifc_system_code": "57395d2bc668496c9c57d8f2b19bd516",
|
||||
"systemCode": "57395d2bc668496c9c57d8f2b19bd516",
|
||||
"appId": "57395d2bc668496c9c57d8f2b19bd516",
|
||||
"secret_key": "opS=K9Parlf&p+JxBOQD2q+zNZa+uXEE",
|
||||
"ifcUrl": "https://dpc.cet.cnooc/prod-api/cenertech-interface-center/IFC2"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# =================== MD5签名计算 ===================
|
||||
def calculate_md5(param, secret_key):
|
||||
return hashlib.md5((secret_key + param).encode('utf-8')).hexdigest()
|
||||
|
||||
|
||||
def create_sign(param_map, secret_key):
|
||||
sorted_keys = sorted(param_map.keys())
|
||||
parts = []
|
||||
|
||||
for key in sorted_keys:
|
||||
val = param_map[key]
|
||||
|
||||
if val is None:
|
||||
parts.append(f"{key}=")
|
||||
continue
|
||||
|
||||
if isinstance(val, list):
|
||||
for item in val:
|
||||
encoded_val = urllib.parse.quote(str(item), encoding='utf-8')
|
||||
parts.append(f"{key}={encoded_val}")
|
||||
continue
|
||||
|
||||
val_str = str(val)
|
||||
|
||||
if key != "REQUEST_BODY_CONTENT":
|
||||
val_str = urllib.parse.quote(val_str, encoding='utf-8')
|
||||
|
||||
parts.append(f"{key}={val_str}")
|
||||
|
||||
param = "&".join(parts)
|
||||
param = "".join(param.split())
|
||||
|
||||
print("参数明文 param:", param)
|
||||
|
||||
sign = calculate_md5(param, secret_key)
|
||||
print("生成签名 sign:", sign)
|
||||
|
||||
return sign
|
||||
|
||||
|
||||
# =================== Python 主程序 ===================
|
||||
if __name__ == '__main__':
|
||||
|
||||
# 选择 dev / prod
|
||||
env = "dev"
|
||||
if len(sys.argv) > 1:
|
||||
env = sys.argv[1]
|
||||
print(f"\n=== 当前环境:{env} ===\n")
|
||||
|
||||
if env not in CONFIG:
|
||||
print("❗ 错误:请使用 python sign.py dev 或 python sign.py prod")
|
||||
sys.exit(1)
|
||||
|
||||
cfg = CONFIG[env]
|
||||
|
||||
systemCode = cfg["systemCode"]
|
||||
appId = cfg["appId"]
|
||||
secret_key = cfg["secret_key"]
|
||||
platform_ifc_system_code = cfg["platform_ifc_system_code"]
|
||||
ifcUrl = cfg["ifcUrl"]
|
||||
|
||||
ts = str(int(time.time() * 1000))
|
||||
randomString = uuid.uuid4().hex
|
||||
|
||||
selector = {
|
||||
"searchKeys": ["9cb864213c6f48ceaf90e98e7ca375e9","3DC1B33E1B5B431E99FA163BF9E86E6A","13336","b353614a47e2425a8a8885d270267407"],
|
||||
"userType": "1",
|
||||
"hasCascade": True
|
||||
}
|
||||
|
||||
request_body_json = json.dumps(selector, separators=(',', ':'))
|
||||
|
||||
param_map = {
|
||||
"systemCode": systemCode,
|
||||
"timestamp": ts,
|
||||
"nonce": randomString,
|
||||
"REQUEST_BODY_CONTENT": request_body_json
|
||||
}
|
||||
|
||||
sign = create_sign(param_map, secret_key)
|
||||
|
||||
apiPath = "/userListByDeptSearch"
|
||||
fullUrl = f"{ifcUrl}/{platform_ifc_system_code}{apiPath}"
|
||||
|
||||
curl = f"""
|
||||
curl -X POST "{fullUrl}" \\
|
||||
-H "Content-Type: application/json" \\
|
||||
-H "systemCode: {systemCode}" \\
|
||||
-H "timestamp: {ts}" \\
|
||||
-H "nonce: {randomString}" \\
|
||||
-H "sign: {sign}" \\
|
||||
-H "App-Id: {appId}" \\
|
||||
-d '{request_body_json}'
|
||||
"""
|
||||
|
||||
print("\n===== 最终 curl 请求 =====\n")
|
||||
print(curl)
|
||||
59
recive_wits.py
Normal file
59
recive_wits.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import socket
|
||||
import time
|
||||
|
||||
HOST = "192.168.1.41"
|
||||
PORT = 9928
|
||||
|
||||
def connect():
|
||||
"""建立 TCP 连接(带重试)"""
|
||||
while True:
|
||||
try:
|
||||
print(f"Connecting to {HOST}:{PORT} ...")
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.connect((HOST, PORT))
|
||||
s.settimeout(5)
|
||||
print("Connected successfully!")
|
||||
return s
|
||||
except Exception as e:
|
||||
print(f"Connection failed: {e}, retrying in 3s...")
|
||||
time.sleep(3)
|
||||
|
||||
|
||||
def receive_wits_data(sock):
|
||||
"""持续接收 WITS 数据(自动处理黏包/拆包)"""
|
||||
buffer = ""
|
||||
|
||||
while True:
|
||||
try:
|
||||
data = sock.recv(4096)
|
||||
|
||||
# 服务器关闭
|
||||
if not data:
|
||||
print("Server closed connection.")
|
||||
return False
|
||||
|
||||
buffer += data.decode(errors="ignore")
|
||||
|
||||
# WITS 多为 \r\n 分隔
|
||||
while "\n" in buffer:
|
||||
line, buffer = buffer.split("\n", 1)
|
||||
line = line.strip()
|
||||
if line:
|
||||
print("Received:", line)
|
||||
|
||||
except socket.timeout:
|
||||
# 正常情况,继续接收即可
|
||||
continue
|
||||
except Exception as e:
|
||||
print("Error:", e)
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
while True:
|
||||
sock = connect()
|
||||
ok = receive_wits_data(sock)
|
||||
sock.close()
|
||||
|
||||
print("Reconnecting in 3 seconds...")
|
||||
time.sleep(3)
|
||||
61
send_wtis.py
Normal file
61
send_wtis.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import socket
|
||||
import random
|
||||
import time
|
||||
|
||||
HOST = "192.168.1.5" # 目标地址
|
||||
PORT = 9929 # 目标端口
|
||||
|
||||
# 你给的示例里出现的所有前四位字段
|
||||
WITS_CODES = [
|
||||
"0105",
|
||||
"0106",
|
||||
"0108",
|
||||
"0112",
|
||||
"0114",
|
||||
"0116",
|
||||
"0118",
|
||||
"0120",
|
||||
"0121",
|
||||
"0122"
|
||||
]
|
||||
|
||||
def random_value(prefix):
|
||||
"""
|
||||
生成类似你收到的数据:
|
||||
- 有些是整数:例如 0105 251114
|
||||
- 有些是浮点:例如 0108 37.26745
|
||||
"""
|
||||
# 随机决定生成整数 or 小数
|
||||
if random.random() < 0.3:
|
||||
# 生成整数(6位左右)
|
||||
value = str(random.randint(100000, 999999))
|
||||
else:
|
||||
# 生成浮点(保留4~5位小数)
|
||||
value = f"{random.uniform(0, 500):.5f}"
|
||||
|
||||
return prefix + value
|
||||
|
||||
|
||||
def send_wits_data():
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.connect((HOST, PORT))
|
||||
print("Connected to target. Sending WITS data...")
|
||||
|
||||
try:
|
||||
while True:
|
||||
for code in WITS_CODES:
|
||||
msg = random_value(code)
|
||||
|
||||
sock.sendall((msg + "\r\n").encode())
|
||||
print("Sent:", msg)
|
||||
|
||||
time.sleep(0.2) # 每条间隔 200ms,可根据需要调整
|
||||
|
||||
except Exception as e:
|
||||
print("Error:", e)
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
send_wits_data()
|
||||
Reference in New Issue
Block a user