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

Real time sending and receiving Python socket client

編輯:Python
The phenomenon and background of the problem

I did it recursively python socket client Realization , But when logging , There are too many duplicate data , What is the cause of this

import astimport jsonimport socketimport timeimport randomfrom settings.record_log import loggersclass Client: def __init__(self, host, port, buffer=1024, timeout=10, encoding='utf-8'): """ initialization Args: host: Connected IP port: Port to establish connection buffer: The size of bytes read at a time timeout: Timeout time encoding: Character encoding format """ self.host = host self.port = port self.buffer = buffer self.timeout = timeout self.encoding = encoding self.tcp_client_socket = None def connect(self): """ The server application establishes a connection Returns: """ # establish tcp Client socket  # 1. AF_INET: Express ipv4 # 2. SOCK_STREAM: tcp Transfer protocol  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): """ send data Args: send_data: Send data body , The default is json Format frame_number: Deduction end condition Returns: nothing """ # Receive data for decoding  send_data = send_data.encode(self.encoding) logger = loggers("../logs/socket_log.txt") # logger.debug(" Data sent by the client :"+ str(send_data)) if frame_number <= 1000: while True: self.tcp_client_socket.send(send_data) # receive data , The maximum number of bytes of data received this time is 1024 server_byte_data = self.tcp_client_socket.recv(self.buffer) # Decode the data  try: server_decode_data = ast.literal_eval(server_byte_data.decode(self.encoding)) #print(" The data of the receiving server is :", server_decode_data, type(server_decode_data)) logger.info(" The data of the receiving server is :"+str(server_decode_data)+str(type(server_decode_data))) except Exception as e: server_decode_data = server_byte_data.decode(self.encoding) # print(" Receive exception - The server data is :", server_decode_data, type(server_decode_data)) logger.error(" Receive exception - The server data is :"+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": # The data structure is unknown  pass if action_type == "observation": pass self.close_client() def close_client(self): """ Close socket Returns: """ self.tcp_client_socket.close() def dict_to_decode(self, data): """ python Dictionary to byte format Args: data: Pass in python Dictionaries Returns: Returns data in byte format """ 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)} } } # Dictionary to byte  byte_data = c1.dict_to_decode(data) c1.send_data(byte_data, 999)
Operation results and error reporting contents

[2022-06-14 22:32:53] [INFO] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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] The data of the receiving server is :{'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

My solution ideas and tried methods

I don't know why I can print the same time log for so many times , How to solve this

What I want to achieve

I want to implement the client Receive the data sent by the server in real time , After processing Then send it to the server


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