Python Forum
asyncio byte data transfer - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: asyncio byte data transfer (/thread-38617.html)



asyncio byte data transfer - gary - Nov-05-2022

I found the below code from my search on the web. The code provides a sample for server implementation using asyncio

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


RE: asyncio byte data transfer - Gribouillis - Nov-05-2022

The only call to write() in the above code is writer.write(data), but data is a bytes instance as can be seen from the line message = data.decode(), so I think the above code actually transmits bytes and not strings.


RE: asyncio byte data transfer - gary - Nov-06-2022

strange, When I print data after reception in python, I get the pattern b'\x01' (Client transmits a "1"). When I retransmit it to client back, it displays the "smily face" symbol, which is equivalent of ALT+1 code. Subsequently When I change the client code for string processing, it works correctly. So I was womdering that python must be transmitting strings and not bytes.
btw my client code is written in C (mingw)

Please clarify.


RE: asyncio byte data transfer - Gribouillis - Nov-06-2022

(Nov-06-2022, 04:35 AM)gary Wrote: When I retransmit it to client back, it displays the "smily face" symbol, which is equivalent of ALT+1 code.
I don't understand that. Try to print the exact hexadecimal codes that the client received.
(Nov-06-2022, 04:35 AM)gary Wrote: I get the pattern b'\x01' (Client transmits a "1")
Note that b'\x01' is not the hexadecimal code for the digit '1' (it is b'\x31').


RE: asyncio byte data transfer - gary - Nov-08-2022

I think you are right. Now it is OK. By the way, writer.write function echoes the data back to client that sends the data. But How can I send to other clients? Can we broadcast to all the clients?


RE: asyncio byte data transfer - Skaperen - Nov-18-2022

each client that connects to be served will have its own separate instance that serves that one client. in order to do things like having a chat between clients, the these server instances will need to be re-designed so that they can do a message exchange. there are many schemes to do that. but. one thing all of them will have is some means to identify each client. you will want to make that be secure which means all communication with clients will be encrypted (use TLS) and clients will login with a password or asymmetric encryption key. then each server code instance will know the user name for who they are serving. and you will need to choose the way you will have it do message exchange (how to no only send a message between instances but also send it to the correct instance). how many instances do you think will be running at one time?