Pybites Logo

Simple tcp client

Level: Intermediate (score: 3)

Your job is to write a simple TCP/IP client which will contact a server and exchange data:

- The server is listening on a specific port and will send the client a fixed sized message when the client first connects.

- The server expects the client to reply with SHA-256 hash of the message the client received.

- The server will keep sending messages and expecting responses.

- The server will close connection at some point. Closing connection is indicated by server sending empty message b"".

Task

1. Create a client that will connect to the server using specific address and port. Address example: ("localhost", 4321)

2. The client will receive a fixed size message from the server.  

3. After each message is received, the client will answer with a confirmation message.

4. The confirmation message is 32 byte long SHA-256 hash generated from the received message.

5. The client continues to respond to messages until the server closes the connection.

6. The server may close the connection at any time, it's up to the client to handle this behavior (that is the client should not crash in such case).

Useful links

socket — Low-level networking interface

Socket Programming in Python (Guide)