程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

實時收發python socket 客戶端

編輯:Python
問題遇到的現象和發生背景

我通過遞歸實現了python socket client 實現,但是在記錄日志時,出現太多重復的數據,這是什麼原因造成的呢

問題相關代碼,請勿粘貼截圖
import astimport jsonimport socketimport timeimport randomfrom settings.record_log import loggersclass Client: def __init__(self, host, port, buffer=1024, timeout=10, encoding='utf-8'): """ 初始化 Args: host: 建立連接的IP port: 建立連接的端口 buffer: 一次讀取的字節大小 timeout: 超時時間 encoding: 字符編碼格式 """ self.host = host self.port = port self.buffer = buffer self.timeout = timeout self.encoding = encoding self.tcp_client_socket = None def connect(self): """ 服務端應用程序建立連接 Returns: """ # 創建tcp客戶端套接字 # 1. AF_INET:表示ipv4 # 2. SOCK_STREAM: tcp傳輸協議 try: self.tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.tcp_client_socket.connect((self.host, self.port)) except socket.error as e: print(e) self.tcp_client_socket.close() time.sleep(5) self.connect() def send_data(self, send_data: json, frame_number): """ 發送數據 Args: send_data: 發送數據體,默認為json格式 frame_number: 推演結束條件 Returns: 無 """ # 接收數據進行解碼 send_data = send_data.encode(self.encoding) logger = loggers("../logs/socket_log.txt") # logger.debug("客戶端發送的數據:"+ str(send_data)) if frame_number <= 1000: while True: self.tcp_client_socket.send(send_data) # 接收數據, 這次接收的數據最大字節數是1024 server_byte_data = self.tcp_client_socket.recv(self.buffer) # 對數據進行解碼 try: server_decode_data = ast.literal_eval(server_byte_data.decode(self.encoding)) #print("接收服務端的數據為:", server_decode_data, type(server_decode_data)) logger.info("接收服務端的數據為:"+str(server_decode_data)+str(type(server_decode_data))) except Exception as e: server_decode_data = server_byte_data.decode(self.encoding) # print("接收異常-服務端數據為:", server_decode_data, type(server_decode_data)) logger.error("接收異常-服務端數據為:"+str(server_decode_data)+ str(type(server_decode_data))) if isinstance(server_decode_data, dict): action_type = server_decode_data.get('action_type') if action_type == "move": server_decode_data['data']['destination']['X'] = random.randint(0, 200) server_decode_data['data']['destination']['Z'] = random.randint(0, 200) self.send_data(self.dict_to_decode(server_decode_data), frame_number) if action_type == "fire": # 該數據結構未知 pass if action_type == "observation": pass self.close_client() def close_client(self): """ 關閉套接字 Returns: """ self.tcp_client_socket.close() def dict_to_decode(self, data): """ python字典轉字節格式 Args: data: 傳入python字典 Returns: 返回字節格式數據 """ data = bytes('{}'.format(data), 'utf-8') data = data.decode(self.encoding) return dataif __name__ == '__main__': c1 = Client('127.0.0.1', 6321) c1.connect() data = { "action_type": "move", "room_id": 1, "camp_id": 0, "data": { "obj_id": 102, "type": 1, "destination": {
"X": random.randint(1, 100), "Y": random.randint(1, 100), "Z": random.randint(1, 100)} } } # 字典轉字節 byte_data = c1.dict_to_decode(data) c1.send_data(byte_data, 999)
運行結果及報錯內容

[2022-06-14 22:32:53] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 58, 'Y': 42, 'Z': 23}}}<class 'dict'>
[2022-06-14 22:32:54] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 179, 'Y': 42, 'Z': 198}}}<class 'dict'>
[2022-06-14 22:32:54] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 179, 'Y': 42, 'Z': 198}}}<class 'dict'>
[2022-06-14 22:32:55] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 41, 'Y': 42, 'Z': 130}}}<class 'dict'>
[2022-06-14 22:32:55] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 41, 'Y': 42, 'Z': 130}}}<class 'dict'>
[2022-06-14 22:32:55] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 41, 'Y': 42, 'Z': 130}}}<class 'dict'>
[2022-06-14 22:32:56] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 14, 'Y': 42, 'Z': 66}}}<class 'dict'>
[2022-06-14 22:32:56] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 14, 'Y': 42, 'Z': 66}}}<class 'dict'>
[2022-06-14 22:32:56] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 14, 'Y': 42, 'Z': 66}}}<class 'dict'>
[2022-06-14 22:32:56] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 14, 'Y': 42, 'Z': 66}}}<class 'dict'>
[2022-06-14 22:32:57] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 195, 'Y': 42, 'Z': 199}}}<class 'dict'>
[2022-06-14 22:32:57] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 195, 'Y': 42, 'Z': 199}}}<class 'dict'>
[2022-06-14 22:32:57] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 195, 'Y': 42, 'Z': 199}}}<class 'dict'>
[2022-06-14 22:32:57] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 195, 'Y': 42, 'Z': 199}}}<class 'dict'>
[2022-06-14 22:32:57] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 195, 'Y': 42, 'Z': 199}}}<class 'dict'>
[2022-06-14 22:32:58] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 85, 'Y': 42, 'Z': 187}}}<class 'dict'>
[2022-06-14 22:32:58] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 85, 'Y': 42, 'Z': 187}}}<class 'dict'>
[2022-06-14 22:32:58] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 85, 'Y': 42, 'Z': 187}}}<class 'dict'>
[2022-06-14 22:32:58] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 85, 'Y': 42, 'Z': 187}}}<class 'dict'>
[2022-06-14 22:32:58] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 85, 'Y': 42, 'Z': 187}}}<class 'dict'>
[2022-06-14 22:32:58] [INFO] 接收服務端的數據為:{'action_type': 'move', 'room_id': 1, 'camp_id': 0, 'data': {'obj_id': 102, 'type': 1, 'destination': {'X': 85, 'Y': 42, 'Z': 187}}}<class 'dict'>

Process finished with exit code -1

我的解答思路和嘗試過的方法

我不知道為什麼會重復打印這麼多次重復的相同時間的日志,這個該如何解決

我想要達到的結果

我想實現客戶端 實時接收服務端發送過來的數據 , 處理後 再發送給服務端


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved