You are on page 1of 6

Balabhadra Niketh

B210688EC
EC01

EC3022D Computer
Networks Winter Semester
2023-24

Computer Networks Assignment-3


Socket Programming

Balabhadra Niketh – B210688EC - EC01


Balabhadra Niketh
B210688EC
EC01

Question:
Build a Web Server in C/Python using Socket programming. The web server
should be able to respond to simple HTTP commands like GET, POST etc.
When a GET request is sent, the server should respond with a page containing
your name and roll number.

Code:
At the web server:
# server.py
1
import socket
2
3
def handle_request(request):
4
if request.startswith("GET"):
5
return "HTTP/1.1 200 OK\nContent-Type: text/html\n\n<html><body><h1>Computer Networks
6
Assignment-2</h1> <br> <h2>Name: Balabhadra Niketh<br>Roll Number: B210688EC</h2>" \
7
"</body></html>"
8
else:
9
return "HTTP/1.1 501 Not Implemented\nContent-Type: text/plain\n\nNot Implemented"
10
11
def start_server(host, port):
12
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
13
server_socket.bind((host, port))
14
server_socket.listen(5)
15
print(f"Server listening on {host}:{port}")
16
17
while True:
18
client_socket, client_address = server_socket.accept()
19
print(f"Client connected from {client_address}")
20
request_data = client_socket.recv(1024).decode()
21 print(f"Request received:\n{request_data}")
22 response = handle_request(request_data)
23 client_socket.sendall(response.encode())
24 client_socket.close()
25
26 if name == " main ":
27 HOST = '127.0.0.1' # Localhost
28 PORT = 8080
29 start_server(HOST, PORT)
Balabhadra Niketh
B210688EC
EC01
Block Diagram at Webserver:

Description of the connection setup:


 This socket setup and connections all require a library “socket” which includes all the
function calls.
 The SOCK_STREAM is used to set up a TCP connection, one of the socket types.
 Initially the server binds with one of the sockets (with a port number) after using the
local host and will be continuously in a loop listening for a connection request from
any of the clients.
 Similarly the client side also follows the same process but instead it sends a
connection request to the server.
 After a successful connection setup the Client sends data (request) to the server, the
server reads the request and sends the required data as a response to the client.
 When the client wants to end the connection with the server, it calls a function:
client_socket.close() Close up the connection.
 Once the server reads the end of the file it will also end its connection with the client.
Balabhadra Niketh
B210688EC
EC01
Function Calls:
 Listen for Connection: The server is always in a loop of listening for any connection
on a specified host and port using socket.bind() and socket.listen()
 Accept Connection : After succesful connection setup the server accepts the
connection using socket.accept()
 Process Request : The server processes the received request, determining its type and
generating an appropriate response using the handle_request() function.
 Send Request : After processing the request, the server sends the response back to
the client using socket.sendall( ).
 Close Connection : Finally, the server closes the connection with the client using
socket.close( ).

After execution of the code:

Client Side Code for Calling GET command:


1 # client.py
2 import socket
3 import webbrowser
4
5 def send_request(host, port, request):
6 client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7 client_socket.connect((host, port))
8 client_socket.sendall(request.encode())
9 response = client_socket.recv(1024).decode()
10 print("Response from server:\n", response)
11 client_socket.close()
12 return response
13
14 if name == "_main_":
15 HOST = '127.0.0.1' # Localhost
16 PORT = 8080
17 request = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"
18 response = send_request(HOST, PORT, request)
19 webbrowser.open_new_tab("http://" + HOST + ":" + str(PORT))
Balabhadra Niketh
B210688EC
EC01
Block Diagram Client side:

Function Calls:
 Send Request : The client sends an HTTP request to the server specifying the host
and port using socket.connect() and socket.sendall().
 Receive Response : The client waits to receive the response from the server using
socket.recv().
 Print Response : Upon receiving the response, the client prints it to the console.
Balabhadra Niketh
B210688EC
EC01
Webpage output :

After sending request at the server side:

Conclusion:
The server efficiently manages incoming HTTP requests by processing them according to
their type and generating suitable responses. Meanwhile, the client effectively sends requests
to the server and receives and prints the responses it receives. This interaction exemplifies a
fundamental client-server architecture, where the server listens for connections, handles
request processing, and sends responses. On the other hand, the client initiates requests and
manages the received responses accordingly, forming a cohesive communication model
between the two entities. This architecture facilitates seamless communication and resource
sharing between clients and servers in distributed systems

You might also like