Lecture#5 Network Programming
Lecture#5 Network Programming
# Introduction #
Network programming in Python involves developing applications that communicate over a
network. This communication can happen between processes on the same machine (inter-
process communication) or between devices connected over a network (inter-device
communication). Python provides several modules and libraries that simplify the process of
working with networks.
1. Socket Programming:
• Sockets are the basic building blocks of network programming. A socket
represents an endpoint for sending or receiving data across a computer network.
Python's socket module provides a way to create and use sockets.
• There are two types of sockets:
1. Server Socket: Listens for incoming connections.
2. Client Socket: Initiates a connection to a server.
2. Server Example:
• A simple server script might look like this:
3. Client-Server Communication:
• To establish communication between a client and a server, you need a server script (as
shown above) and a client script. The client script connects to the server and
sends/receives data.
4. Protocols: Network communication often involves the use of protocols such as TCP
(Transmission Control Protocol) or UDP (User Datagram Protocol). TCP provides reliable,
connection-oriented communication, while UDP is a connectionless and faster protocol.
5. HTTP Communication: For web-related network programming, the requests library is
commonly used to send HTTP requests. The http.server module provides a basic HTTP
server for handling requests.
6. Asynchronous Programming: Python's asyncio module allows you to write asynchronous
code, which is useful for handling multiple network connections simultaneously without the
need for threading.
7. Error Handling and Security: Network programming requires robust error handling to
deal with issues such as connection failures. Additionally, security considerations, such as
encryption, should be taken into account for secure communication.
8. Libraries and Frameworks: Python offers higher-level libraries and frameworks like
Twisted and Tornado for building more complex network applications.
In summary, network programming in Python involves creating applications that can send and
receive data over a network. Whether you're building a simple client-server application or a
more complex networked system, Python provides the tools and libraries to make the
development process more straightforward.
This is just a brief overview, and network programming can get quite complex depending on
your specific requirements. The choice between synchronous and asynchronous programming,
as well as the protocols and libraries used, will depend on your application's needs.
# A Dynamic Server #
A dynamic server in the context of network programming typically refers to a server that
dynamically generates responses based on incoming requests. This often involves handling
various types of requests and generating appropriate responses, such as serving web pages or
processing different types of data.
Here's an example of a simple dynamic server using Python and the built-in http.server
module. This server handles HTTP GET requests and dynamically generates responses based
on the requested URL:
Save this script, run it, and then open a web browser and navigate to http://localhost:8080 and
http://localhost:8080/about. You'll see that the server dynamically generates different
responses based on the requested path.
This example uses the http.server module, which is a simple HTTP server included in the
Python standard library. The DynamicHandler class is a subclass of
BaseHTTPRequestHandler that overrides the do_GET method to handle GET requests.
Depending on the requested path, the server generates different responses.
In a more complex scenario, a dynamic server might interact with databases, perform
calculations, or handle various types of requests from clients. Libraries like Flask or Django
can also be used to build dynamic web servers with more features and capabilities.
1. Create a Socket:
• The socket.socket function is used to create a new socket. socket.AF_INET
specifies the address family (IPv4), and socket.SOCK_STREAM specifies the
socket type (TCP).
2. Connect to the Server:
• The connect method is used to establish a connection to the server. The server's
address (hostname and port) is specified in the server_address variable.
3. Send a Message:
• The sendall method is used to send a message to the server. The message is
encoded into bytes using encode() before sending.
4. Receive Response:
• The recv method is used to receive data from the server. The received data is
then decoded from bytes using decode().
5. Close the Socket:
• The close method is called to close the socket connection after the
communication is complete.
To run this client program, you would typically have a server program running on the specified
address and port (in this case, 'localhost' and 8080). The server program would be designed to
handle incoming connections, process requests, and send back responses. The example I
provided earlier in this conversation demonstrates a basic server that can work with this client.
1. Create a Socket:
• Similar to the client, the socket.socket function is used to create a new socket.
socket.AF_INET specifies the address family (IPv4), and
socket.SOCK_STREAM specifies the socket type (TCP).
2. Bind the Socket:
• The bind method associates the socket with a specific network interface and
port. In this example, the server binds to the address 'localhost' and port 8080.
3. Listen for Connections:
• The listen method puts the socket in listening mode, waiting for incoming
connections. The argument (1 in this case) specifies the maximum number of
queued connections.
4. Accept Connections in a Loop:
• The accept method blocks until a client connects. When a connection is
accepted, it returns a new socket (client_socket) and the address of the client
(client_address).
5. Receive Data from the Client:
• The server receives data from the client using the recv method.
6. Send a Response to the Client:
• The server sends a response back to the client using the sendall method.
7. Close the Connection:
• After handling the client's request and sending a response, the server closes the
connection with the client using the close method.
To run this server program, execute it in a terminal or command prompt. Then, you can run the
client program (provided in a previous response) to connect to this server.