parent
c220aaf30f
commit
af0089209e
@ -0,0 +1,28 @@
|
||||
import asyncio
|
||||
import struct
|
||||
|
||||
|
||||
class Client:
|
||||
BUFFER_SIZE = 1024
|
||||
|
||||
def __init__(self, ip: str, port: int):
|
||||
self.__ip = ip
|
||||
self.__port = port
|
||||
|
||||
async def connect(self):
|
||||
reader, writer = await asyncio.open_connection(self.__ip, self.__port)
|
||||
await self.handle(reader, writer)
|
||||
|
||||
async def handle(self, reader, writer):
|
||||
try:
|
||||
while True:
|
||||
size, = struct.unpack('<L', await reader.readexactly(4))
|
||||
message = await reader.readexactly(size)
|
||||
print(f"{message.decode('utf-8') =}")
|
||||
|
||||
except asyncio.CancelledError:
|
||||
print('Something went wrong')
|
||||
finally:
|
||||
writer.close()
|
||||
await writer.wait_closed()
|
||||
|
@ -0,0 +1,8 @@
|
||||
import asyncio
|
||||
|
||||
from .client import Client
|
||||
|
||||
if __name__ == '__main__':
|
||||
client = Client('127.0.0.1', 5555)
|
||||
asyncio.run(client.connect())
|
||||
|
Loading…
Reference in new issue