TCP Socket Programming in Python

In this Python Socket Programming article we want to learn about TCP Socket Programming in Python, so TCP (Transmission Control Protocol) is reliable and connection oriented protocol that provides a stream of data between two networked devices. Python provides  powerful and easysocket API that allows you to write TCP client and server programs. in this article we want to cover the basics of TCP programming in Python.

 

 

Introduction to TCP Socket Programming

Socket programming is a way of connecting two nodes on network to communicate with each other. socket is a software endpoint that establishes communication link between two processes over network. TCP is reliable and connection oriented protocol that establishes  connection between two networked devices and provides a stream of data that is guaranteed to be delivered in order and without loss or duplication. Python provides socket API that makes it easy to write TCP client and server programs. socket API is a set of functions and constants that allows you to create, configure and use sockets for network communication.

 

 

TCP Socket Programming in Python: A Simple Example

This is simple example of TCP client and server in Python, this is our client code and you can save it in client.py file.

 

 

This is our server.py file

In this example server listens on port 8000 for incoming connections. hen a client connects, the server sends welcome message to the client and then closes the connection. client connects to the server and receives the welcome message.

 

 

To run this code you need to two terminal first run server.py file and after that client.py file

 

Server in Terminal

 TCP Socket Programming in Python
TCP Socket Programming in Python

 

 

 

Client in Terminal

 TCP Socket Programming in Python
TCP Socket Programming

 

 

Learn More

 

 

Leave a Comment