Net-informations.com
SiteMap  | About    

Pyhon Socket programming

Socket programming is one of the most fundamental technologies of computer network programming . A socket is an endpoint of a two-way communication link between two programs running on the network. The client and server can communicate by writing to or reading from their sockets. Python has quite an easy way to start with the socket interface. Pythons's socket module provides access to the BSD socket interface . It is available on all modern Unix systems, Windows, Mac OS X, BeOS, OS/2, and probably additional platforms.

The Python Socket Programming has two sections:

  1. Python Server Socket Program
  2. Python Client Socket Program
Foundations of Python Network Programming
What is socket programming in Python

Python Server Socket Program

The Server Socket Program here is a Python Console based Application . This program act as a Server and listening to clients request from Port No. 8080.

When the Server Socket accept a request from the Client side, it reads the data from client and also it write the response to the connected client program .

Python Server Socket Example (Server.py)

Python Client Socket Program

The Client socket is connected to the Port 8080 of the Python Server Socket Program , and the IP Address ("127.0.0.1") of the server machine. Here we give as 127.0.0.1 , because the Server and Client running on the same machine. If the client program running on other machine, then you can give the IP Address of that machine.

When the Python Client program starts , it will connect to the Python Server Socket Program and waiting input from client side. When you type the message it will send to the server and then you can see the reply messages from server side too.

Python Socket Client Example(client.py)

How to run this program ?

Create Python Server Socket Program (Server.py) and Python Client Socket Program (client.py) in two separate files. When you finish coding, first you have to start Python Server Socket Program from DOS prompt (console) , then you will get a message " Server Started..." and "Waiting for client request.." in your DOS screen, where the server program is running .

Next step is to start Python Client Socket Program from DOS prompt (console) in the same computer or other computers on the same network . When you start the client program , it will establish a connection to the Server and send message ("This is from Client") from client side. After receiving message from client side, the server send message to the client "Successfully Connected to Server!!". That's...now you can see you client program and server program communicate each other.

Asynchronous Socket Programming (Read-Write both side)

In the above example, you can just send one message from client as well as just one return message from server also. If you want to connect and communicate client and server repeatedly, you should implement a while loop for sending and receiving data each other.

Python Server Socket Example (Server.py)

Python Socket Client Example(client.py)

How to run this program ?

Create Python Server Socket Program (Server.py) and Python Client Socket Program (client.py) in two separate files. When you finish coding, first you have to start Python Server Socket Program from DOS prompt (console) , then you will get a message " Server Started..." and "Waiting for client request.." in your DOS screen, where the server program is running .

Next step is to start Python Client Socket Program from DOS prompt (console)in the same computer or other computers on the same network . When you start the client program , it will establish a connection to the Server and send message ("This is from Client") from client side. After receiving message from client side, server waiting for the input from server side. Then, you can type message from server side and press enter key. At the same time client receive that message and waiting for the input from client side. Again you can type the message from client side and press enter key. Now you can see the server and client communicate repeatedly. This communication you can continue up to send "bye" from client side.









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.