Python Forum
asyncio byte data transfer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
asyncio byte data transfer
#1
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
likes this post
Reply
#2
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.
Reply
#3
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.
Reply
#4
(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').
Reply
#5
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?
Reply
#6
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python:How to read a recived message that idinfies the client Byte by Byte using UDP learn 2 5,938 Dec-01-2017, 09:47 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020