Socket
Programming In Python
Welcome my friends, my new
tutorial on how we could create our own
messaging system by the use of socket programming in Python. In this tutorial we will learn how we could send data
from device-to-device, client-to-server by the
using of socket programming in Python.
So let’s start our tutorial
:-
Contents :
·
Definition of
Socket Programming ?
·
Why We Have To
Use Sockets to Send Data ?
·
Socket
Programming In Python
·
Socket Client
Server In Python
·
Socket Client
In Python
·
How Do You
Share The Data Between Clients?
Definition
of Socket Programming ?
Socket Programming is a
program that enables two sockets to send and receive data bi-directionally at the same moment.
It works by connecting two
sockets (or nodes) together and allowing them to communicate in real time, and
it is a great opportunity to create biggest projects on messaging field.
Why We Have To Use Sockets to Send Data ?
Online applications that
need socket programming to operate in realtime with greatly benefits from the
implementation of sockets in their network codes.
Some
examples of applications that uses socket programming given here :-
·
Web pages like
Facebook, Twitch etc.
·
Multiplayer
games Counter Strike,PubG etc.
·
Chat apps like WhatsApp, WeChat, Slack etc.
Python, unlike JavaScript,
is a language that executes synchronously. This is why asyncio was developed to
make Python more robust, particularly for the nature of socket programming.
With streaming sockets, data
can be sent or received at any time. In case your Python program is in the
middle of executing some code, other threads can handle the new socket data.
Libraries like asyncio implement multiple threads.
Socket
Programming In Python
Python provides a socket
class so developers can easily implement socket objects in their projects code.
To implement sockets in
our program we have to follow some steps in our projects code :-
1. Import Socket Library
To use a socket object in
your program, start off by importing the socket library. No need to install it
with a package manager, it comes out of the box with Python.
import socket
2. Build Socket Objects
Now we can create socket
objects in our project :-
socket =
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
This code creates a socket object
that we are storing in the “socket” variable. The constructor is provided a family and type
parameter respectively. The family
parameter is set to the default value, which is the Address Format Internet.
The type parameter is set to
Socket Stream, also the default which enables “sequenced,
reliable, two-way, connection-based byte streams” over TCP.
3.Open and Close Connection
Once we have an initialized
socket object, we can use some methods to open a connection, send data, receive
data, and finally close the connection.
sock.connect(('0.0.0.0',
8080))
sock.send("Twenty-five
bytes to send")
sock.recv(4096)
sock.close()
Socket
Client Server In Python
Now that
we know a few methods for transmitting bytes, let’s create a client and server program
in Python :-
import socket
socket =
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind(('0.0.0.0',
8080))
serv.listen(5)
while True:
conn, addr = serv.accept()
client = ''
while True:
data = conn.recv(4096)
if not data:
break
client += data
print (client)
conn.send("Server
connected")
conn.close()
print ('Server
disconnected')
How Does
it Work ?
This code makes a socket
object, and binds it to localhost’s port 8080 as a socket server. When clients
connect to this address with a socket connection, the server listens for data,
and stores it in the “data” variable.
Then, the program logs the
client data using “print,” and then sends a string to the client: Server
connected.
Socket
Client In Python
Example
of the client socket :-
import socket
client =
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('0.0.0.0',
8080))
client.send("Client
here")
server = client.recv(4096)
client.close()
print (server)
How Does
it Work ?
This client opens up a
socket connection with the server, but only if the server program is currently
running. To test this out yourself, you will need to use 2 terminal windows at
the same time.
Next, the client sends some
data to the server: Client here
Then the client receives
some data it anticipates from the server.
Congratulations !!! You can
now get started streaming data between clients and servers using some basic
Python network programming.
How Do
You Share The Data Between Clients?
Sending data between 2 or
more client devices over the internet is tricky. Due to protections implemented
by network security, not all devices connected to the world wide web have a
publicly accessible internet protocol (IP) address.
This means that the Python
code that we implemented will not be 100% reliable for sending peer-to-peer
data in our realtime applications.
So, how do we achieve
reliability and speed when transmitting peer-to-peer data?
This can be accomplished
using a server in the middle :-
·
Client devices
using the internet can connect to a server with a public IP address or a
website domain like www.domain.com.
·
Then, this
broker in the middle can pass messages routed to 1 or many clients.
This is the the end of our tutorial guys.
If you liked this post, then please distribute it among your friend circle or on the social media platforms you use.
Thank you Guys and Good Bye...
Thank you Guys and Good Bye...
Keep learning,
0 Comments