Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Networking application with Python: Note that please explain and create a manual for your code and walk me through its execution process and put comments

Networking application with Python:

image text in transcribed

Note that please explain and create a manual for your code and walk me through its execution process and put comments in the code. I have to explain it to my teaching assistant

thanks again.

the code template for this question:

#!/usr/bin/env python3

# created by Ali J on March 7th 2017

import argparse

import sys import itertools import socket from socket import socket as Socket

# A simple web server

# Issues: # Ignores CRLF requirement # Header must be

def main():

# Command line arguments. Use a port > 1024 by default so that we can run # without sudo, for use as a real server you need to use port 80. parser = argparse.ArgumentParser() parser.add_argument('--port', '-p', default=2080, type=int, help='Port to use') args = parser.parse_args()

# Create the server socket (to handle tcp requests using ipv4), make sure # it is always closed by using with statement. with Socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:

# The socket stays connected even after this script ends. So in order # to allow the immediate reuse of the socket (so that we can kill and # re-run the server while debugging) we set the following option. This # is potentially dangerous in real code: in rare cases you may get junk # data arriving at the socket. server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

server_socket.bind(('', args.port)) server_socket.listen(1)

print("server ready")

while True:

with server_socket.accept()[0] as connection_socket: request = connection_socket.recv(1024).decode('ascii') reply = http_handle(request) connection_socket.send(reply.encode('ascii'))

print(" Received request") print("======================") print(request.rstrip()) print("======================")

print(" Replied with") print("======================") print(reply.rstrip()) print("======================")

return 0

def http_handle(request_string): """Given a http requst return a response

Both request and response are unicode strings with platform standard line endings. """

assert not isinstance(request_string, bytes)

# Fill in the code to handle the http request here. You will probably want # to write additional functions to parse the http request into a nicer data # structure (eg a dict), and to easily create http responses.

raise NotImplementedError

pass

if __name__ == "__main__": sys.exit(main())

In this assignment, you will develop a simple Web server in Python that is capable of processing only one request. Specifically, your Web server will create a connection socket when contacted by a client (browser); receive the HTTP request from this connection; parse the request to determine the specific file being requested; get the requested file from the server's file system; create an HTTP response message consisting of the requested file preceded by header lines; and send the response over the TCP connection to the requesting browser. If a browser requests a file that is not present in your server, your server should return a "404 Not Found" error message. In the companion Web site, we provide the skeleton code for your server. Your job is to complete the code, run your server, and then test your server by sending requests from browsers running on different hosts. If you run your server on a host that already has a Web server running on it, then you should use a different port than port 80 for your Web server

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

Students also viewed these Databases questions

Question

Explain strong and weak atoms with examples.

Answered: 1 week ago

Question

Explain the alkaline nature of aqueous solution of making soda.

Answered: 1 week ago

Question

Comment on the pH value of lattice solutions of salts.

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago