In network programming, python plays a very important role. Python provides full support for encoding and decoding data and network protocol in its standard library. Writing a network program in python is very simple compared to any other language like C++.
Python gives access to two levels of network services which are:
- First, it provides low-level access. In low-level access, we can access and use the basic socket support of OS with the help of python libraries.
- Second, it provides high-level access. In high-level access, we can access the application-level networks like HTTP, FTP, etc. using python libraries.
Now before moving forward with the main topic. Let’s get familiar with some smaller concepts like Socket.
What are Sockets?
Sockets can be defined as the end-point of a communication process. In any communication process, we have end-points between two similar processes, two different processes and within a common process. These end-points are called sockets. It uses different protocols for finding the connection type of communication between client and server. It can be implemented over different channels like TCP, UDP, etc.
Python provides socket methods which help in providing virtual sockets to the communicating processes.
s = socket.socket (socket_family, socket_type, protocol=0)
Let’s understand the vocabulary of Socket
- Domain: It is basically the family of protocols used for transportation.
- type: It represents the type of communication between two end-points.
- protocol: This is typically zero, but it can be used to identify the protocol used in the network.
- hostname: The identifier of the network. It can be any name given to the host.
- port: It is a specific number given to a port or a string value. It is used when the client calls for more than one port to reach the server.
Let’s understand some server methods
- Server Socket Methods
- s.bind(): This method is used to bind the address to sockets.
- s.listen(): This method is used to set up and start a TCP listener.
- s.accept(): This method is used for accepting the TCP client connection.
- Client Socket Methods
- s.connect(): This method is used to initiate server connections for TCP.
- General Socket Methods
- s.recv(): This method is used to receive the TCP message.
- s.send(): This method is used to transmit the TCP message.
- s.recvfrom(): This method is used to receive the UDP message.
- s.sendto(): This method is used to transmit the UDP message.
- s.close(): This method is used for closing the sockets.
- socket.gethostname(): This method is used for returning the hostname.
A Simple Server
# Importing the socket module import socket # Creating a socket object sct = socket.socket() # Getting name for the local machine host = socket.gethostname() # Reserving a port for the service. port = 12445 # Bind to the port sct.bind((host, port)) # waiting for the client connection. sct.listen(7) while True: # Establishing the connection with the client. cs, adrs = sct.accept() print 'Got connection from', adrs cs.send('Thank you for connecting to the server') cs.close()
In the above code, we made a socket object and reserved a port on our pc. After that, we have bounded our server to a specific port. In the above code, we have passed an empty string so that the server can listen to incoming connections from other computers as well. We have used 7 in the listen() method, which means that 7 connections are kept waiting until the server is busy, and after that, if an 8th socket tries to connect, the connection will be refused. In the end, we have used a while loop for accepting all incoming connections.
A Simple Client
This is client.py file which is used to set up the client-server.
# Importing the socket module import socket # Creating a socket object sct = socket.socket() # Getting a local machine name host = socket.gethostname() # Reserving a port for services port = 12445 sct.connect((host, port)) print sct.recv(1024) sct.close()
Now, if we run this server.py in the background and then run client.py, we will get the connection.
# These commands will help in starting the server $ python server.py & # After the server is started, we can run the client $ python client.py
Got connection from ('127.0.0.1', 48437) Thank you for connecting