Computer Networks Assignment-2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Ganta Karthikeya Kushal Kumar

B210699EC

Computer Networks Assignment

Socket Programming

Q) 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:

import socket

def handle_request(client_socket):
request_data = client_socket.recv(1024)
decoded_request = request_data.decode('utf-

8') http_method = decoded_request.split(' ')

[0]

if http_method == 'GET':
response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
response += "<html><body><h1>Hello!!</h1><h2>Name: Gannta
Karthikeya
Kushal Kumar</h2><h2>Roll Number:
B210699EC</h2></body></html>" else:
response = "HTTP/1.1 405 Method Not Allowed\r\n\r\n"

client_socket.sendall(response.encode('utf-8'))
client_socket.close()

def run_server():
server_socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM) server_socket.bind(('127.0.0.1', 8000))
server_socket.listen(5)
print("Server is listening on port 8000...")

while True:
client_socket, _ = server_socket.accept()
print("Received connection from:",

client_socket.getpeername()) handle_request(client_socket)
Ganta Karthikeya Kushal Kumar
B210699EC

run_server()

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.
Ganta Karthikeya Kushal Kumar
B210699EC

 Once the server reads the end of the file it will also end its connection with the client.

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:

# client.py
import socket
import
webbrowser
Ganta Karthikeya Kushal Kumar
B210699EC

def send_request(host, port, request):


client_socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM) client_socket.connect((host, port))
client_socket.sendall(request.encode())
response = client_socket.recv(1024).decode() print("Response from server:\
n", response)
client_socket.close()
return response

if name == "_main_":
HOST = '127.0.0.1' # Localhost
PORT = 8080
request = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" response =
send_request(HOST, PORT, request)
webbrowser.open_new_tab("http://" + HOST + ":" + str(PORT))

Block Diagram Client side:


Ganta Karthikeya Kushal Kumar
B210699EC

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.

Output:

In the server:

Name and Roll number is displayed

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