Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Networking: I need help to write this home work in python! Using TCP sockets, you will write a simplified version of a HTTP client and

Networking:

I need help to write this home work in python!

Using TCP sockets, you will write a simplified version of a HTTP client and server. The client program will use the HTTP protocol to fetch a file from the server using the HTTP GET method, cache it, and then subsequently use conditional GET operations to fetch the file only if it has been modified.

The HTTP client will perform the following functions:

  1. Take in a single command line argument that specifies a web url containing the hostname and port where the server is running, as well as the name of the file to be fetched, in the appropriate format. Example: localhost:12000/filename.html
  2. If the file is not yet cached, use a HTTP GET operation to fetch the file named in the URL
    1. Print out the contents of the file
    2. Cache the file
  3. If the file is cached, use a Conditional GET operation for the file named in the URL
    1. If the server indicates the file has not been modified since last downloaded, print output saying so (no need to print file contents in this case)
    2. Otherwise, indicate that the file has been modified, and print and cache new contents

The HTTP server will perform the following functions:

  1. Read a command-line argument specifying IP address and port server is to listen on e.g. 127.0.0.1 12000
  2. Open a TCP socket and listen for incoming HTTP Get and Conditional GET requests from one or more HTTP Clients at above address and port
  3. In the case of a HTTP Get request:
    1. Read the named file and return a HTTP GET Response, including the Last-Modified header field
  4. In the case of a HTTP Conditional Get Request:
    1. If the file has not been modified since that indicated by If-Modified-Since, return the appropriate Not Modified response (return code 304)
    2. If the file has been modified, return the file contents as in step 2
  5. In the case that the named file does not exist, return the appropriate Not Found error (return code 404)
  6. The server must ignore all header fields in HTTP Requests it does not understand

Simplifying Assumptions:

  • Only GET and Conditional GET requests need be supported in client and server
  • Only a subset of header fields need to be supported in HTTP Requests and Responses (see Message Format section)
  • However, the client and server must ignore all header fields it does not understand. For example, a real web browser will send many more header fields in GET requests than those expected to be implemented by the server. The server MUST ignore these fields and continue processing as if these fields were not part of the GET request. The server MUST NOT report an error in these cases.

Cache Implementation:

  • The cache must be implemented as a file so that it persists across client instantiations
  • The file(s) used to implement the cache must include cache in the filename so that it is easy to distinguish e.g cache.txt
  • The program must work as per the test cases across multiple client instantiations, one per test case.
  • The client program must work if the cache file does not exist (in which case, the implication is no files have been cached)
  • Note that this means the file may have existed after previous runs of your program, but has since been deleted prior to client restarting

Test Cases:

Enable wireshark during all the following test cases. (One wireshark .pcap is fine for the test cases in this section, but the .pcap must show the test cases in this order.) Run the client four times, once for each test case.

  1. Run client when web object not cached (or no cache exists): Using your HTTP client, download the contents of a text-based html file named filename.html from your HTTP server using the appropriate URL. Example: localhost:12000/filename.html. The client must:
    1. Print out the contents of the header in the HTTP Request
    2. Print out the contents of the header in the HTTP Response (should indicate a 200 OK Response)
    3. Print out file contents (you can print as is. No formatting is required)
  2. Run client when web object cached, but not modified on server: Using your HTTP Client, send a conditional GET request to your HTTP server. The client must:
    1. Print out the contents of the header in the HTTP Request
    2. Print out the contents of the header in the HTTP Response (should indicate a 304 Not Modified Response)
  3. Run client when web object cached, but modified on server: Using your HTTP Client, send a conditional GET request to your HTTP server. The client must:
    1. Print out the contents of the header in the HTTP Request
    2. Print out the contents of the header in the HTTP Response.
    3. Display the new file contents
  4. Web object does not exist: Using your HTTP Client, send a GET request for a filename that does not exist. The client must:
    1. Print out the contents of the header in the HTTP Request
    2. Print out the contents of the header in the HTTP Response

Test Cases for extra credit:

Using a web browser, such as Firefox or Chrome (note: Safari web browser may not implement the conditional GET as expected), perform the same test cases as above on your server. Enable wireshark during all the following test cases. One wireshark .pcap is fine for the web browser test cases.

  1. Enter the URL in the web browser search bar and press . The web browser should print the contents of the file downloaded from your server.
  2. Re-enter the URL in the web browser search bar and press . The web browser should show the same web page contents as in step 1 (assuming the file has not been modified.), and the wireshark trace should show a Conditional Get and a Not Modified response.
  3. Modify the file. Re-enter the URL in the web browser search bar and press . The web browser should now show the updated web contents.
  4. Enter a non-existent URL, and browser should indicate Not found

Message Format:

HTTP messages are encoded as strings in a specific format defined according to the HTTP specification[1,2,3]. References on the HTTP Message Format can be found as follows:

  • Please see textbook (Section 2.2.3) or lecture slides web.pdf on Moodle for general format of HTTP Request and Response messages and some examples, specifically slides 16-17 and 20-22.
  • See textbook section 2.2.5 and slide 32 for information on HTTP Conditional Get
  • Also, look at the wireshark trace provided as part of the assignment (HTTP_Conditional_Get_Example.pcapng) for an example of HTTP GET and Conditional GET requests.

As part of this assignment, your HTTP Client and HTTP Server programs are only expected to handle the following header fields:

HTTP Client GET Request Message:

Your GET Request must include the following:

  • Request line containing method (GET) , object (from URL) and version (HTTP1.1)
  • Host: includes hostname (and port, if specified, separated by :)
  • Blank line: signifies ends of header, expressed by \

Example:

GET /filename.html HTTP/1.1

Host: localhost:12000

HTTP Server Response to Client GET Request (assuming file exists):

The response from the HTTP Server must include the following:

  • Status line including version (HTTP1.1), status code (200), and status phrase (OK)
  • Date: header field containing current date and time in the following format (must be UTC/GMT time zone):
    • Example of Date format: Mon, 23 Jan 2017 15:55:47 GMT
  • Last-Modified: header field containing date and time file was last modified. Must follow same format as Date: above
  • Content-Length: length of data in bytes
  • Content-Type (can be hard-coded): text/html; charset=UTF-8
  • Blank line: signifies ends of header
  • Body: Contents of requested file

Example:

HTTP/1.1 200 OK

Date: Sun, 04 Mar 2018 21:24:58 GMT

Last-Modified: Fri, 02 Mar 2018 21:06:02 GMT

Content-Length: 75

Content-Type: text/html; charset=UTF-8

First Line
Second Line
Third Line
COMPLETE

HTTP Client Conditional GET Request Message:

Your GET Request must include the following:

  • Request line containing method (GET), object (from URL) and version (HTTP1.1)
  • Host: Same as in GET request
  • If-Modified-Since: Echo back value of Last-Modified time in HTTP GET Response
  • Blank line: signifies ends of header

Example:

GET /filename.html HTTP/1.1

Host: localhost:12000

If-Modified-Since: Fri, 02 Mar 2018 21:06:02 GMT

HTTP Server Conditional Response Message (Not Modified):

HTTP/1.1 304 Not Modified

Date: Sun, 04 Mar 2018 21:24:58 GMT

HTTP Server Response when file not found:

HTTP/1.1 404 Not Found

Date: Sun, 04 Mar 2018 21:24:58 GMT

Programming Hints: See references [4,5,6]

Get current time in UTC/GMT time zone and convert to string in HTTP format:

import datetime, time

t = datetime.datetime.now(timezone.utc)

date = time.strftime("%a, %d %b %Y %H:%M:%S %Z ", t)

Determining a files modification time (in seconds since 1 Jan, 1970 on Unix machines)

import os.path

secs = os.path.getmtime(filename)

Convert above time to UTC /GMT (returns a time tuple):

import time

t = time.gmtime(secs)

Convert above time tuple to a string in HTTP format:

last_mod_time = time.strftime("%a, %d %b %Y %H:%M:%S GMT ", t)

Convert a date/time in string format back to time tuple and seconds since 1 Jan, 1970

t = time.strptime(last_mod_time, "%a, %d %b %Y %H:%M:%S %Z ")

secs = time.mktime(t)

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_2

Step: 3

blur-text-image_3

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

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions

Question

Specify the HRM importance of Griggs v. Duke Power. P-968

Answered: 1 week ago