数据转换.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import socket
  2. import time
  3. import re
  4. def send_recv(sock, hex_str, name, wait=0.05):
  5. data = bytes.fromhex(hex_str)
  6. print(f"\n[{name}] 发送: {len(data)}字节")
  7. sock.send(data)
  8. time.sleep(wait)
  9. try:
  10. resp = sock.recv(65536)
  11. print(f"[{name}] 收到: {len(resp)}字节")
  12. return resp
  13. except socket.timeout:
  14. print(f"[{name}] 超时")
  15. return None
  16. def main():
  17. HOST = "47.108.151.236"
  18. PORT = 211
  19. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  20. sock.settimeout(10)
  21. try:
  22. sock.connect((HOST, PORT))
  23. print("连接成功")
  24. # UUID
  25. uuid = "04da00005400000008000000260000007b00430034003500460044003300330043002d0037003900300046002d0034003400450041002d0042003300410044002d003400300030003600410044003500310036003800370037007d00"
  26. send_recv(sock, uuid, "UUID")
  27. # 会话ID
  28. session = "03da000018000000030000000000000008000000040000006900640030003200"
  29. send_recv(sock, session, "会话")
  30. # 查询时间
  31. time_q = "02da00005e0000000300000000000000030000002e01000003000000010000000b000000ffff030000000200000003000000000000000800000010000000730065006c0065006300740020006700650074006400610074006500280029000300000063000000"
  32. send_recv(sock, time_q, "时间查询")
  33. send_recv(sock, session, "心跳1")
  34. # 表结构
  35. struct_q = "02da0000740000000300000000000000030000002e01000003000000010000000b000000ffff03000000020000000300000000000000084000001b000000730065006c0065006300740020002a002000660072006f006d0020007a0062006b00200077006800650072006500200031003d0030000300000063000000"
  36. send_recv(sock, struct_q, "表结构")
  37. send_recv(sock, session, "心跳2")
  38. # 1500查询
  39. query1 = "02da0000520000000300000000000000030000002e01000003000000010000000b000000ffff03000000020000000300000000000000080000000b0000003200360030003600300039003000330037004c004100020000001500"
  40. resp1 = send_recv(sock, query1, "查询1(1500)")
  41. if resp1:
  42. text1 = resp1[4:].decode('gbk', errors='replace')
  43. print("\n" + "=" * 70)
  44. print("提取的客户信息")
  45. print("=" * 70)
  46. # 姓名
  47. name_match = re.search(r'片([\u4e00-\u9fff]{2,3})', text1)
  48. if name_match:
  49. print(f"\n姓名: {name_match.group(1)}")
  50. # 电话
  51. phone_match = re.search(r'1[3-9]\d{9}', text1)
  52. if phone_match:
  53. print(f"电话: {phone_match.group()}")
  54. # 验光数据
  55. degree_pattern = r'(-?\d+\.\d+)(-?\d+\.\d+)'
  56. degrees = re.findall(degree_pattern, text1)
  57. optometry = []
  58. for sph, cyl in degrees:
  59. if sph.startswith('-') or cyl.startswith('-'):
  60. if len(optometry) < 2:
  61. optometry.append({'球镜': sph, '柱镜': cyl})
  62. if optometry:
  63. print(f"\n验光数据:")
  64. for i, d in enumerate(optometry, 1):
  65. eye = "右眼" if i == 1 else "左眼"
  66. print(f" {eye}: 球镜 {d['球镜']}, 柱镜 {d['柱镜']}")
  67. # 消费明细 - 直接从原始文本提取
  68. print(f"\n消费明细:")
  69. # 查找所有包含"镜片"的行
  70. lines = text1.split('\n')
  71. detail_count = 0
  72. for line in lines:
  73. if '镜片' in line:
  74. detail_count += 1
  75. # 提取完整的行(保留原始内容,只过滤掉太短的)
  76. if len(line) > 20:
  77. print(f" {detail_count}. {line.strip()}")
  78. elif '镜架' in line:
  79. detail_count += 1
  80. if len(line) > 10:
  81. print(f" {detail_count}. {line.strip()}")
  82. # 1800查询
  83. send_recv(sock, session, "心跳3")
  84. query2 = "02da0000520000000300000000000000030000002e01000003000000010000000b000000ffff03000000020000000300000000000000080000000b0000003200360030003600300039003000330037004c004100020000001800"
  85. resp2 = send_recv(sock, query2, "查询2(1800)")
  86. # 关闭
  87. close_pkt = "05da0000080000000300000000000000"
  88. send_recv(sock, close_pkt, "关闭")
  89. except Exception as e:
  90. print(f"错误: {e}")
  91. finally:
  92. sock.close()
  93. print("\n连接已关闭")
  94. if __name__ == "__main__":
  95. main()