12345678910111213141516171819202122232425262728293031 |
- """
- python3 /home/server/repositories/repositories/sri-project.demo-py/3rdparty/xclient/xtcp.py
- """
- import asyncio
- import struct
- async def send_message(reader, writer, values):
- data = struct.pack('!hh', *values)
- writer.write(data)
- await writer.drain()
- print(f"Sent values: {values}")
- size = struct.calcsize('!hh')
- response = await reader.read(size)
- if response:
- received_values = struct.unpack('!hh', response)
- print(f"Received response: {received_values}")
- async def start_client(host='127.0.0.1', port=20917, messages=[(100, 200)]):
- reader, writer = await asyncio.open_connection(host, port)
- try:
- for values in messages:
- await send_message(reader, writer, values)
- finally:
- writer.close()
- await writer.wait_closed()
- if __name__ == "__main__":
- messages = [(100, 200), (300, 400), (500, 600)] # 示例消息列表
- asyncio.run(start_client(messages=messages))
|