Nov-05-2022, 03:01 PM
I found the below code from my search on the web. The code provides a sample for server implementation using asyncio
Server echoes the data back to the client, but writers.write transmits the data as string. i have a client program written in C that accepts only data format in bytes. How can I change the above program to tansmit bytes instead of string
import asyncio async def handle_echo(reader, writer): data = await reader.read(100) message = data.decode() addr = writer.get_extra_info('peername') print("Received %r from %r" % (message, addr)) print("Send: %r" % message) writer.write(data) await writer.drain() print("Close the client socket") writer.close() loop = asyncio.get_event_loop() coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop) server = loop.run_until_complete(coro) print('Serving on {}'.format(server.sockets[0].getsockname())) loop.run_forever()]
Server echoes the data back to the client, but writers.write transmits the data as string. i have a client program written in C that accepts only data format in bytes. How can I change the above program to tansmit bytes instead of string