Question
please use python In this assignment, you will learn the basics of socket programming for TCP connections in Python: how to create a socket, bind
please use python
In this assignment, you will learn the basics of socket programming for TCP connections in Python: how to create a socket, bind it to a specific address and port, as well as send and receive an HTTP packet. You will also learn some basics of HTTP header format. You can only use Python3.
You will develop a web server that handles one HTTP request at a time. Your web server should accept and parse the HTTP request, get the requested file from the servers file system, create an HTTP response message consisting of the requested file preceded by header lines, and then send the response directly to the client. If the requested file is not present in the server, the server should send an HTTP 404 Not Found message back to the client.
Code
Attached you will find the skeleton code for the Web server. You are to complete the skeleton code. The places where you need to fill in code are marked with #Fill in start and #Fill in end. Each place may require one or more lines of code.
Part 1: Running the Server
Put the attached HTML file (named HelloWorld.html) in the same directory in which the server webserver.py runs. Run the server program. Determine the IP address of the host that is running the server (e.g., 128.238.251.26 or localhost). From another host, open a browser and provide the corresponding URL. For example: http://128.238.251.26:6789/HelloWorld.html. You can open a browser in the same host where the server runs and use the following http://localhost:6789/HelloWorld.html.
HelloWorld.html is the name of the file you placed in the server directory. Note also the use of the port number after the colon. You need to replace this port number with the port number that was assigned to you. In the above example, we have used port number 6789. The browser should then display the contents of HelloWorld.html. If you omit :6789, the browser will assume port 80 (why?), and you will get the web page from the server only if your server is listening at port 80.
Then try to get a file that is not present on the server (e.g., test.html). You should get a 404 File Not Found message.
Skeleton Python Code
Skeleton Python Code for the Web Server is attached.
Part 2: The client program
Write your own HTTP client to test your server. Your client will connect to 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 sent is a GET method. The client should take command line arguments specifying the server IP address or hostname, the port at which the server is listening, and the HTTP file name (e.g., test.html or HelloWorld.html). The following is an input command format to run the client. webclient.py
E.g.,
python3 webclient.py localhost 6789 HelloWorld.html
Bonus Exercise (10%)
Currently, the webserver handles only one HTTP request at a time. Implement a multithreaded server that is capable of serving multiple requests simultaneously. Using threading, create a main thread in 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.
Rules
- DO NOT output any debugging information or other informational messages to the screen. Doing so will cost you points!
- The program must run on Linux in the Virtual Lab or equivalent machine.
- Name your server program file name webserver.py, the client file name webclient.py, and include the HelloWorld.html in your submission. Please note that the filenames are case sensitive.
- Use the port number that is assigned to you (here).
- Create a README file that contains a complete listing/explanation of what files are present in the directory and any specific instructions to run your code. Further, include your citations.
- For the README file, use a plain text editor like TextPad, Notepad++, or Linux vi, vim, nano, etc. Do NOT MS Word or any Office Editor.
Submission
- Submission is accepted only in the D2L dropbox during the submission window.
What to Hand in
You will hand in the complete server and the client code along with your browsers screenshots, verifying that you receive the contents of the HTML file from the server. Make sure you follow the Rules.
Your code will be evaluated on one of the universitys virtual lab machines based on the following evaluation rubric.
Evaluation Rubric (50 pts)
The points for the assignment are as follows:
- 15 pts - Code mechanics (server and client)
- 10 pts - Code properly commented.
- 5 pts - Code has an appropriate comment block explaining the code, student name, and star-id.
- 20 pts - server program
- 10 pts - server responds with the correct HTTP code and sends the file back to the client to show in the browser when the requested file is found
- 10 pts - server responds with the correct HTTP code when the requested file is not found and shows a 404 File Not Found message in the browser.
- 15 pts - client program
- 5 pts - the program takes the correct CLI arguments (i.e., python3 webclient.py
) in the correct order and handles invalid user input appropriately. - 5 pts - the program correctly handles the HTTP OK code
- 5 pts - the program correctly handles the HTTP 404 code
- 5 pts - the program takes the correct CLI arguments (i.e., python3 webclient.py
How-to
1- How take an argument from the CLI?
Using python
import sys
arg1 = sys.argv[1]
arg2 = sys.argv[2]
...
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started