Python Socket How To Connect TCP Client To Server

In this Python Socket programming i want to show you How To Connect TCP Client To Server.

 

 

Also you can check Python Socket Programming Articles

 

1: Python Socket Programming How To Create Socket

2: Python Socket How To Get Local Machine IP Address

3: Python Socket How To Get Website IP Address

 

Basics Of Socket 

Network programming in any programming language can begin with sockets. But
what is a socket? Simply put, a network socket is a virtual end point where entities
can perform inter-process communication. For example, one process sitting in a
computer, exchanges data with another process sitting on the same or another
computer. We typically label the frst process which initiates the communication
as the client and the latter one as the server.Python has quite an easy way to start with the socket interface.

In order to understand  this better, let’s see the big picture first. In the following figure, a flow of client/server
interaction is shown. This will give you an idea of how to use the socket API.

 

Client Server Interaction Through Socket
Client Server Interaction Through Socket

 

In the interaction between a typical client and a server, the server process has to work
a bit more, as you may have thought. After creating a socket object, the server process
binds that socket to a particular IP address and port. This is much like a telephone
connection with an extension number. In a corporate office, after a new employee
has been allocated with his desk phone, usually he or she will be assigned to a new
extension number. So, if anybody makes a phone call to this employee, the connection
can be established using his phone number and extension. After the successful binding,
the server process will start listening for a new client connection. For a valid client
session, the server process can accept the request of the client process. At this point, we
can say that the connection between the server and the client has been established.

Then the client/server enters into the request/response loop. The client process
sends data to the server process, and the server process processes the data and
returns a response to the client. When the client process finishes, it exits by closing
down the connection. At that moment, the server process probably goes back to the
listening state.
The above interaction between client and server is a very simplified representation
of the actual reality.

 

 

So now this is the complete code for Python Socket How To Connect TCP Client To Server

 

 

So this line of code is for creating socket in python programming language , also we are handling exceptions for this

 

 

Also in here we are going to get the input of host name and port from the user

 

 

 

In here we are going to connect our target host to the port

 

 

 

So now run the complete code and this will be the result

 

Python Socket How To Connect TCP Client To Server
Python Socket How To Connect TCP Client To Server

 

 

 

Also you can watch the complete video for this article 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

2 thoughts on “Python Socket How To Connect TCP Client To Server”

  1. Nice introductory tutorials on sockets. Thanks! Could you please give us a tip on implementing socket connections from PyQt5? Is it the same or do you need to use some PyQt variations to do it? This would make a great addition to your excellent PyQt5 tutorials as well, particularly how to exchange data between a PyQt5 app and another embedded computer (a Raspberry Pi or Arduino, etc).

    • yea particularity, this will be different in Pyqt5, because pyqt5 has it is own features for network and socket programming i will make a tutorial on that

Comments are closed.

Codeloop
Share via
Copy link
Powered by Social Snap
×