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.
server.bind((LOCALHOST, PORT)) server.listen(1)

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)
import socket LOCALHOST = "127.0.0.1" PORT = 8080 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((LOCALHOST, PORT)) server.listen(1) print("Server started") print("Waiting for client request..") while True: clientConnection,clientAddress = server.accept() print("Connected clinet :" , clientAddress) data = clientConnection.recv(1024) print("From Client :" , data.decode()) clientConnection.send(bytes("Successfully Connected to Server!!",'UTF-8')) clientConnection.close()

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.
client.connect(SERVER, PORT)
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)
import socket SERVER = "127.0.0.1" PORT = 8080 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((SERVER, PORT)) client.sendall(bytes("This is from Client",'UTF-8')) data = client.recv(1024) print(data.decode()) client.close()

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)
import socket LOCALHOST = "127.0.0.1" PORT = 8080 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((LOCALHOST, PORT)) server.listen(1) print("Server started") print("Waiting for client request..") clientConnection,clientAddress = server.accept() print("Connected clinet :" , clientAddress) msg = '' while True: in_data = clientConnection.recv(1024) msg = in_data.decode() if msg=='bye': break print("From Client :" , msg) out_data = input() clientConnection.send(bytes(out_data,'UTF-8')) print("Client disconnected....") clientConnection.close()
Python Socket Client Example(client.py)
import socket SERVER = "127.0.0.1" PORT = 8080 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((SERVER, PORT)) client.sendall(bytes("This is from Client",'UTF-8')) while True: in_data = client.recv(1024) print("From Server :" ,in_data.decode()) out_data = input() client.sendall(bytes(out_data,'UTF-8')) if out_data=='bye': break client.close()

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.