This project implements a simple TCP server in Python that listens for incoming connections, logs a message upon connection, and then closes the connection.
- Listens on a specified port (default: 8080)
- Logs a message to the console when a client connects, showing the client's IP address and port
- Closes the connection after logging the message
- Graceful shutdown with Ctrl+C
- Python 3
No additional installation is required beyond having Python installed on your system.
-
Clone this repository or download the
tcp_server.py
file. -
Open a terminal or command prompt.
-
Navigate to the directory containing
tcp_server.py
. -
Run the server with the following command:
python tcp_server.py
-
The server will start and display a message indicating it's listening:
Server is listening on 0.0.0.0:8080
-
To test the server, you can use a tool like
telnet
ornc
(netcat) from another terminal:nc localhost 8080
or
telnet localhost 8080
-
You should see a message in the server's console indicating a client has connected, showing their IP address and port.
-
To stop the server, press Ctrl+C in the terminal where the server is running.
You can modify the host and port in the tcp_server.py
file by changing the default values in the start_server
function:
def start_server(host='0.0.0.0', port=8080):
This project is open source and available under the MIT License.
Contributions, issues, and feature requests are welcome. Feel free to check issues page if you want to contribute.
Parth Jain
- Python Socket Programming documentation