What is Socket?
Socket in a term used in network programming most commonly said as Node. Almost every programming language like C, C++, Java or Python etc have capability to connect multiple system connected with each other by the purpose of sending or receiving message or communicating with the socket. Sockets are used nearly everywhere.
so, in the most simple term we can say socket is medium through which we can connect through any port no. Computer have different types of port available to connect with other machine in network. similarly all website over internet has available port that's why we can open any website and see the content else not possible to open any website if doesn't have any public port.
Every public websites have port 80 open for HTTP access, port 22 for SSH (Secure shell), 20 and 21 open for File transfer Protocol (FTP) and website with HTTPS has 443 open port.
so if try to access our website www.codersarts.com which is open to access port 443 which is most secure.
In this Article we will learn how to write python code to connect with port over internet.
What is socket Programming?
Socket programming is a way of connecting two nodes on a network to communicate with each other.
So, in short we can say that one node is called client node or client socket which send request to server and other one is called server socket or server which listen the client request and respond accordingly. “client” socket - an endpoint of a conversation, and a “server” socket, which is more like a switchboard operator.
The client application (your browser, for example) uses “client” sockets exclusively; the web server it’s talking to uses both “server” sockets and “client” sockets.
we are going to learn here INET (i.e. IPv4)sockets and this is accountable for more than 99% of the sockets in use. And STREAM (i.e. TCP) sockets
Let's see below python code to creating socket.
Creating a socket
#create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#now connect to the web server on port 80
# - the normal http port
s.connect(("www.codersarts.com", 80))
# - And Localhost connection like this
#LOCALHOST = 127.0.0.1
#s.connect((LOCALHOST, 80))
When the connect completes, the sockets can be used to send in a request for the text of the page. The same socket will read the reply, and then be destroyed. That’s right, destroyed. Client sockets are normally only used for one exchange (or a small set of sequential exchanges).
Server sending message to client
Example 1: Implementing a simple client server in Python socket programming:
import socket
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = socket.gethostname()
port = 8889
server_socket.bind((host,port))
server_socket.listen(5)
#This asks for permission on Windows
while True:
client_sockets,addr=server_socket.accept()
msg = "Greeting from Server"
print(f"Connected to {str(addr)}")
client_sockets.send(msg.encode("ascii"))
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = socket.gethostname()
port = 8889
s.connect((host,port))
msg = s.recv(1024)
s.close()
Example 2: with localhost server
import socket
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
LOCALHOST = '127.0.0.1'
port = 9999
server_socket.bind((LOCALHOST,port))
server_socket.listen(5)
print("Server started...")
while True:
client_sockets,addr=server_socket.accept()
print(f"Connected to {str(addr)}")
msg = "Greeting from Server"
client_sockets.send(msg.encode("ascii"))
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
LOCALHOST = '127.0.0.1'
port = 9999
s.connect((LOCALHOST,port))
print("Client requested:")
msg = s.recv(1024)
print(msg.decode())
s.close()
decode function is used to convert byte object to string object
How to Run:
Step 1: First run server (server.py)
Step 2: After running server run the client(client.py)
Server sending message to client and vice-versa
#python #pythonProgrammingHelp #socketprogrammingInPython #pythonAssignmentHelp #pythonHomeworkHelp