Class 6: Networking
Introduction to Network Programming in Python
Networking is a fundamental concept in computer science and programming. In Python, network programming involves writing code that communicates with other computers over a network, whether it be through the internet or a local network. Python provides several modules that make network programming relatively simple. The most commonly used modules are socket and socketserver.
Working with Sockets
A socket is an endpoint of a two-way communication link between two programs running on a network. A socket is characterized by an IP address, a port number, and a protocol. The IP address identifies the machine on the network, while the port number identifies the program. The protocol specifies the rules and format for data exchange. In Python, the socket module provides the necessary classes and methods for working with sockets.
Implementing Client-Server Applications
A client-server application is a software architecture model in which the client sends requests to the server, which then provides a response. In Python, client-server applications can be implemented using sockets. The server listens for incoming requests on a specified port and IP address, and the client connects to the server by specifying the same IP address and port number. Once the connection is established, the client can send requests to the server, and the server can respond with data.
Practice:
Implement a Simple Chat Server Using Sockets in Python
Let’s put our networking skills to the test and implement a simple chat server using sockets in Python. The chat server will allow multiple clients to connect to the server and send messages to each other. Here’s a step-by-step guide on how to implement this:
Step 1: Create a Server Socket First, we need to create a socket object for the server. We’ll use the socket module and specify the socket type as SOCK_STREAM, which is a TCP socket.
import socket
# create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Step 2: Bind the Server Socket Next, we need to bind the server socket to a specific IP address and port number. We can use the bind() method to do this. In this example, we’ll bind to the localhost (127.0.0.1) and port number 8000.
# bind the socket to a public host and a well-known port
server_socket.bind(('127.0.0.1', 8000))
Step 3: Listen for Incoming Connections Now, we need to listen for incoming connections on the server socket. We can use the listen() method to do this. In this example, we’ll set the maximum number of queued connections to 5.
# become a server socket
server_socket.listen(5)
Step 4: Accept Client Connections When a client connects to the server socket, we need to accept the connection and create a new socket object for that client. We can use the accept() method to do this. This method returns a new socket object and the client address.
while True:
# accept connections from outside
(client_socket, client_address) = server_socket.accept()
Step 5: Handle Client Connections Once we have accepted a client connection, we need to handle that connection. In this example, we’ll create a new thread to handle each client connection. This allows multiple clients to connect to the server simultaneously.
import threading
def handle_client(client_socket):
# handle the client connection
pass
while True:
# accept connections from outside
(client_socket, client_address) = server_socket.accept()
# create a new thread to handle the client connection
client_thread = threading.Thread(target=handle_client, args=(client_socket,))
client_thread.start()
Step 6: Send and Receive Messages Now that we have a thread to handle each client connection, we can send and receive messages between the client and server. We can use the recv() method to receive messages from the client, and the send() method to send messages to the client.
def handle_client(client_socket):
while True:
# receive data from the client
data = client_socket.recv(1024)
if not data:
break
# send the data back to the client
client_socket.send(data)
Step 7: Close the Connection Finally, when the client connection is closed, we need to close the client socket.
def handle_client(client_socket):
while True:
# receive data from the client
data = client_socket.recv(1024)
if not data:
break
# send the data back to the client
client_socket.send(data)
# close the client socket
client_socket.close()
That’s it! With these steps, we have implemented a simple chat server using sockets in Python.