Professional Documents
Culture Documents
Cnlab52021 Ce 40
Cnlab52021 Ce 40
Cnlab52021 Ce 40
Lab Title: Web Server using Socket Programming Teacher Name: Darakhshan Abdul Ghaffar
Lab Evaluations:
CLO2, CLO3 Analyze the Internet core functions using diverse online available
tools Build simulations to measure the network parameters
Completeness
(5)
Total /10
Very Good 8 L2 Submitted the lab tasks but have weak understanding
#Fill in start
serverPort = 6789
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
#Fill in end
while True:
print('Ready to serve...')
try:
message = connectionSocket.recv(1024).decode()
filename = message.split()[1]
f = open(filename[1:])
outputdata = f.readlines()
#Fill in start
connectionSocket.send('HTTP/1.1 200 OK\n\n'.encode())
#Fill in end
connectionSocket.send(outputdata[i].encode())
connectionSocket.close()
except IOError:
#Fill in start
#Fill in end
#Fill in start
connectionSocket.close()
#Fill in end
serverSocket.close()
threading, first create a main threadin which your modified server listens for clients at
a fixed port. When it receives a TCP connection request from a client, it will set up the
TCP connection through another port and services the client request in a separate thread.
There will be a separate TCP connection in a separate thread for each request/response
pair.
Python code :
import sys
import threading
def handle_request(connectionSocket):
try:
message = connectionSocket.recv(1024).decode()
filename = message.split()[1]
f = open(filename[1:])
outputdata = f.readlines()
connectionSocket.send(outputdata[i].encode())
connectionSocket.close()
except IOError:
connectionSocket.close()
def main():
serverPort = 6789
serverSocket.bind(('', serverPort))
serverSocket.listen(5)
while True:
print('Ready to serve...')
t = threading.Thread(target=handle_request, args=(connectionSocket,))
t.start()
main()
Bonus Task
2. Instead of using a browser, write your own HTTP client to test your server. Your client
will connectto the server using a TCP connection, send an HTTP request to the server,
and display the server response as an output. You can assume that the HTTP request
The client should take command line arguments specifying the server IP address or host
name, the port at which the server is listening, and the path at which the requested object
is stored at the server. The following is an input command format to run the client.
Python code :
# BONUS TASK
# import socket
# hostname = socket.gethostname()
# print("Hostname:", hostname)
# ip_address = socket.gethostbyname(hostname)
# print("IP Address:", ip_address)
import socket
import sys
try:
client_socket.connect((server_host, server_port))
client_socket.sendall(request.encode())
response = client_socket.recv(4096)
print(response.decode())
except socket.error as e:
finally:
client_socket.close()
if name == " main ":
if len(sys.argv) != 4:
sys.exit(1)
server_host = sys.argv[1]
server_port = int(sys.argv[2])
filename = sys.argv[3]
And command prompt shows that request is successful and the server has returned the requested
content.