Python Forum
I may need some testing / review help with socket server exercise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I may need some testing / review help with socket server exercise
#1
Hi Friends!

Briefly: here is the exercise I've created couple days ago but for which I feel my skills are not adequate to test it properly, thus any remarks on the statement flaws, probable errors or hacks, deficiencies, issues with automated check - are welcome (and most wished for).

Longer story:

It so happened that recently I worked in a company where people were working on some automated testing system for students (job candidates) and I participated in a discussion where managers quarreled with programmers because testing exercises were too "remote" from "practical needs".

Now it happened so that I myself run this small site with coding problems for some years and I felt this reproach is correct. I tried to recollect some interview questions or test projects I met in my career and at last decided on this one. In my idea the user (student) is to create code to listen both UDP and TCP simultaneously. In my opinion this requires multithreading (but perhaps this is wrong?) - and here is a small catch when testing code opens two connections
one after another but tries to exchange the data on the second of them first (in my opinion this should stuck with single-threaded TCP server).

You see, regretfully I am not much in Python - actually I thought about this very small exercise perhaps for a week or more - despite the checking code (which runs in separate thread (daemon) is perhaps 20 lines and my "reference" solution for the server about another 20 lines.
Reply
#2
Mixing UDP and TCP handling is a practical challenge, especially when testing networking skills. Multithreading does seem like a reasonable approach to handle both simultaneously, but async programming might also be worth considering in Python, given its strengths in managing I/O operations.
Reply
#3
Hi Friends, thanks for answers. There was some feedback already, as couple days ago one of the users identified small bug which made some confusion and since then there are several solutions as I see.

Quote: but async programming might also be worth considering in Python,

Do you mean handling a list of sockets and checking in a loop which of them has some data to be processed etc, without spawning dedicated thread for each?
Reply
#4
If you need help with testing or reviewing a socket server exercise, feel free to share the details! Whether it's the code, a specific issue, or guidance on testing strategies, I'd be happy to assist you with it. Just let me know what you're working on and what kind of support you need.

Also, if you're preparing for a link removed, practicing questions related to socket programming can be valuable. You might be asked to write a simple client-server socket program or explain how you would handle multiple connections using select(), poll(), or threading. We can go over common interview questions or test exercises if that would be helpful!
Larz60+ write Sep-11-2024, 08:26 PM:
link removed
Reply
#5
(Jun-19-2024, 08:12 PM)rodiongork Wrote: In my idea the user (student) is to create code to listen both UDP and TCP simultaneously. In my opinion this requires multithreading (but perhaps this is wrong?) - and here is a small catch when testing code opens two connections

No, it does not require multithreading nor multiprocessing. If you write low level code, then you use select and later default selector (which has a better interface). The last step is to learn how Asyncio works.

The server can run multiple sockets with only one thread. CPU-Intensive computations should not be done in the same Python-Process.


Example partly based on this: https://docs.python.org/3/library/asynci...ng-streams
import asyncio


class UdpProto(asyncio.DatagramProtocol):
    """
    UDP does not support asyncio.start_server, because it is not connection based.
    You've to write your own Protocol and/or Transport
    """
    def __init__(self):
        self.transport = None

    def datagram_received(self, data, addr):
        message = data.decode()
        print(f"Received from {addr} following message: {message}")
        self.transport.sendto(f"Re: {message}".encode(), addr)
    
    def connection_made(self, transport):
        self.transport = transport


async def handle_echo(reader, writer):
    data = await reader.read(100)
    message = data.decode()
    addr = writer.get_extra_info('peername')

    print(f"Received {message!r} from {addr!r}")

    print(f"Send: {message!r}")
    writer.write(data)
    await writer.drain()

    print("Close the connection")
    writer.close()
    await writer.wait_closed()

async def main():
    local_addr = ('127.0.0.1', 8888)
    tcp_server = await asyncio.start_server(handle_echo, *local_addr)
    udp_transport, _ = await asyncio.get_event_loop().create_datagram_endpoint(UdpProto, local_addr)

    addrs = ', '.join(str(sock.getsockname()) for sock in tcp_server.sockets)
    print(f'Serving on {addrs}')

    async with tcp_server:
        try:
            await tcp_server.serve_forever()
        except asyncio.CancelledError:
            pass
    print("TCP-Server has been closed")

    udp_transport.close()
    print("UDP-Server has been closed")    


if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

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