For this project you are going to write a simple web server using Python sockets only (i.e. no Flask, not even http.server). Your server should have the following functionality:
- Bind to TCP port 4300 on 127.0.0.2 and accept 1 request at a time.
- Serve a single file, alice30.txt.
- Log details of each incoming request to webserver.log.
- Return 405 Method Not Allowed error for any method other than GET
- Return 404 Not Found error for any request other than /alice30.txt.
- Send the content of alice30.txt to the client along with proper response header.
A typical request header sent by a browser (Chrome in this case) looks as follows:
GET /alice30.txt HTTP/1.1
Host: 127.0.0.2:4300
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Use curl
to initiate a POST request and see a 405 error:
curl -X POST http://127.0.0.2:4300/alice30.txt
Log file should contain the following information:
- Time of the request.
- Requested file.
- IP address of the client.
- Browser vendor and version.
2018-11-05 09:10:52.906984 | /alice.txt | 127.0.0.1 | Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
2018-11-05 09:12:18.072352 | /alice30.txt | 127.0.0.1 | Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
2018-11-05 09:15:00.526359 | /alice.txt | 127.0.0.1 | Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
2018-11-05 09:21:58.869701 | /alice.txt | 127.0.0.1 | Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
2018-11-05 09:22:06.622828 | /alice30.txt | 127.0.0.1 | Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
Your server must include the following headers in the response:
- HTTP version: 1.1
- Response code: 200 OK
Content-Length
: length of alice30.txt.Content-Type
: plain text (not html).Date
: current dateLast-Modified
: Friday, August 29, 2018 11:00 AMServer
: must include your name
The response header sent by your server should look as follows (date and server are going to be different):
HTTP/1.1 200 OK
Content-Length: 148545
Content-Type: text/plain; charset=utf-8
Date: Sun Nov 4 23:25:40 2018
Last-Modified: Wed Aug 29 11:00:00 2018
Server: CS430-ROMAN