Computer Networks
Budget: ₹600 – ₹1,500 INR
Using the server.py and client.py code below as a guide, modify them to accommodate the following
use case:
server.py
import socket
HOST = "127.0.0.1" #Standard loopback interface address (localhost)
PORT = 65432 #Port to listen on
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: #Create a new socket
using the given address family,
# socket type, and protocol number
s.bind((HOST, PORT)) #Bind the socket to an address and port number. The socket
must not already be bound.
s.listen() #Enable a server to accept connections.
conn, addr = s.accept() #Accept a connection. The socket must be bound to an
address and listening for connections.
#The return value is a pair (conn, address) where conn is a new socket object
usable to send and receive data on
#the connection, and address is the address bound to the socket on the other
end of the connection.
with conn: #with keyword is used for unmanaged resources such as socket stream
or file stream, used for exception
#handling
print(f"Connected by {addr}") #prints the client's connection address
while True: #looping the input stream to receive all byte data from the
client
data = conn.recv(1024) #receives the data from the client
if not data:
break #breaks out once no more data to receive
conn.sendall(data) #send the data back to the client (echo it back).
The socket must be connected to a remote
#socket (client's socket).
client.py
#Client program to send data to a server via a socket, and then have that same data
echoed back.
import socket
HOST = "127.0.0.1" #The server's host name or IP address
PORT = 65432 #The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: #Create a new socket
using teh given address famiily, socket
#type and protocol number
s.connect((HOST, PORT)) #Connect to a remote socket at the address and port
s.sendall(b"Hello networking!") #Send data to the socket. The socket must be
connected to a remote socket. Data type
#is byte
data = s.recv(1024) #Receive data from the socket. The return type is a bytes
object representing the data received.
#The maximum amount of data to be received in each chunk is 1024 as specified.
print(f"Received {data!r}") #print to console
-- The client will send an LA Times (https://www.latimes.com/) article URL to the server, the server will
then respond back to the client with the article’s title. The format of the message that should be
displayed must match that of the example below.
This is specifically asking for the article’s title. Not the page name or anything else. See
the example below for further clarification.
You can use whatever LA Times article you want for your testing, we’ll only test with LA Times articles,
as indicated in the “Constraints” section below.
- The input LA Times article URL must be able to be inputted to the client via the command
prompt or Terminal.
This assignment must be done using the Python version 3.10 or later programming version
Do not have the code in any class (e.g. “class Solution:”)
Example:
The following is run from the command prompt. This is how your code will be graded. Your program
absolutely needs to be able to be run from the command prompt or Terminal, otherwise you will get
zero credit. No exceptions. The output of your code should display in the command prompt or Terminal
console.
Example:
>python client.py https://www.latimes.com/travel/newsletter/2022-11-10/rainy-hikes-safety-los-
angeles-the-wild
L.A.’s 'rainy season' can be the most beautiful time to hike. But read these tips first
Further clarification screenshot:
Constraints:
• Library restrictions: you can only import the follow libraries:
o client.py:
▪ import socket
▪ import sys
o server.py:
▪ import socket
▪ import requests
▪ from bs4 import BeautifulSoup
use case:
server.py
import socket
HOST = "127.0.0.1" #Standard loopback interface address (localhost)
PORT = 65432 #Port to listen on
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: #Create a new socket
using the given address family,
# socket type, and protocol number
s.bind((HOST, PORT)) #Bind the socket to an address and port number. The socket
must not already be bound.
s.listen() #Enable a server to accept connections.
conn, addr = s.accept() #Accept a connection. The socket must be bound to an
address and listening for connections.
#The return value is a pair (conn, address) where conn is a new socket object
usable to send and receive data on
#the connection, and address is the address bound to the socket on the other
end of the connection.
with conn: #with keyword is used for unmanaged resources such as socket stream
or file stream, used for exception
#handling
print(f"Connected by {addr}") #prints the client's connection address
while True: #looping the input stream to receive all byte data from the
client
data = conn.recv(1024) #receives the data from the client
if not data:
break #breaks out once no more data to receive
conn.sendall(data) #send the data back to the client (echo it back).
The socket must be connected to a remote
#socket (client's socket).
client.py
#Client program to send data to a server via a socket, and then have that same data
echoed back.
import socket
HOST = "127.0.0.1" #The server's host name or IP address
PORT = 65432 #The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: #Create a new socket
using teh given address famiily, socket
#type and protocol number
s.connect((HOST, PORT)) #Connect to a remote socket at the address and port
s.sendall(b"Hello networking!") #Send data to the socket. The socket must be
connected to a remote socket. Data type
#is byte
data = s.recv(1024) #Receive data from the socket. The return type is a bytes
object representing the data received.
#The maximum amount of data to be received in each chunk is 1024 as specified.
print(f"Received {data!r}") #print to console
-- The client will send an LA Times (https://www.latimes.com/) article URL to the server, the server will
then respond back to the client with the article’s title. The format of the message that should be
displayed must match that of the example below.
This is specifically asking for the article’s title. Not the page name or anything else. See
the example below for further clarification.
You can use whatever LA Times article you want for your testing, we’ll only test with LA Times articles,
as indicated in the “Constraints” section below.
- The input LA Times article URL must be able to be inputted to the client via the command
prompt or Terminal.
This assignment must be done using the Python version 3.10 or later programming version
Do not have the code in any class (e.g. “class Solution:”)
Example:
The following is run from the command prompt. This is how your code will be graded. Your program
absolutely needs to be able to be run from the command prompt or Terminal, otherwise you will get
zero credit. No exceptions. The output of your code should display in the command prompt or Terminal
console.
Example:
>python client.py https://www.latimes.com/travel/newsletter/2022-11-10/rainy-hikes-safety-los-
angeles-the-wild
L.A.’s 'rainy season' can be the most beautiful time to hike. But read these tips first
Further clarification screenshot:
Constraints:
• Library restrictions: you can only import the follow libraries:
o client.py:
▪ import socket
▪ import sys
o server.py:
▪ import socket
▪ import requests
▪ from bs4 import BeautifulSoup