Simulate A Client-Server Application
Python Network Programming Using Sockets
Hey Buddy 👋, welcome to my little world. Every developer must learn and understand web technologies and if you are more into cyber security, an interest I am developing lately, then you really need to know some networking concepts.
Today, we shall create our own client-server application to simulate the real-world application.
I suppose you already know how the Internet Works, If you do not, please check out my article that summarises it in just ten lines.
So What Is A Client-Server Application?
It is an application that runs on the client-side and accesses the remote server for information usually over HTTP-Requests.
A client usually does not share any of its resources, but it requests content or service from a server. Clients, therefore, initiate communication sessions with servers, which await incoming requests.
Examples of computer applications that use the client-server model are email, network printing, and the World Wide Web. See Gmail Example Below
Python Implementation:
There are two types of sockets:
🔸 UDP - User Datagram Protocol
🔸 TCP - Transmission Control Protocol
In this tutorial, we shall focus on TCP since you will likely encounter this 99% more than UDP and more reliable than UDP. TCP is a connection-oriented protocol, whereas UDP is a connectionless protocol.
Socket Module:
The primary socket API functions and methods in this module are:
socket() ->Creates a socket.
bind() ->Associates a local address with a socket.
listen() ->Establishes a socket to listen for incoming connections.
accept() ->Accepts connection
connect() ->Establishes a connection to a peer.
send() ->Sends data on a connected socket.
recv() ->Receives Incoming data
gethostname() ->returns the host name of the current system
close() ->Closes connection
Now create a client.py
file and we code this: Don't Copy!
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = socket.gethostname()
PORT = 35004
s.bind((HOST, PORT))
print("---Waiting For Connection")
s.listen(5)
while True:
conn, addr = s.accept()
print("Got Connection from, ", addr)
conn.send("Server Saying Hi...".encode())
conn.close()
Do not be in a rush to run that script as it will return errors since we have not built the server yet:
Create a server.py
file and code this: Do Not Just Copy!
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = socket.gethostname()
PORT = 35004
s.bind((HOST, PORT))
print("---Waiting For Connection")
s.listen()
while True:
conn, addr = s.accept()
print("The Computer Currently Connected is , ", addr)
conn.send("Server Saying Hi...".encode())
conn.close()
Now, run server.py first in one terminal and then client.py in another. Output:
Explanation 💨:
The socket.socket(socket.AF_INET, socket.SOCK_STREAM)
is for creating an instance of the socket
object which can also be written as socket.socket()
since the arguments are optional.
AF_INET
is an address family that is used to designate the type of addresses that your socket can communicate with (in this case, Internet Protocol v4 addresses).
SOCK_STREAM
means that it is a TCP socket. SOCK_DGRAM
means that it is a UDP socket.
The parameter to socket.recv(recv_size)
is the maximum length of the message to read at once.
Go visit the docs to read more functions and what you can do with the socket module.
Conclusion
We just implemented a basic client-server application with just one module. In real-life applications, it is more complicated and a server has to handle multiple clients, send data, receive data etc.
Now get your hands dirty by subscribing, sharing and engaging this blog and following me on Twitter .
Ronnie Atuhaire 🤓