Description
In this assignment, you need to use python3 asyncio stream to create a web file browser. HTTP/1.0 should be used, because HTTP/1.1 supports keep alive, which makes our implementation more complex.
When running in a directory, the home page of your server should be the list of files and sub directories. The functions should include: browsing directory, jumping, and open files. Editing directory and files are not asked to supported.
An example is given like this.
1. Logic
(1) Your server should only support GET/HEAD method. For more details about the difference between GET and HEAD method, please read the slides about http. When receive other method (POST etc.), you should return an error code 405 Method Not Allowed.
(2) Remember to add Connection: close to your response header.
(3) When receive a request path, check if it is a directory. You might want to add ‘./’ to the client path to make sure you are working on current directory.(pathlib might be helpful for doing path operation. https://docs.python.org/3/library/pathlib.html)
① path = ‘./’ + header.get(‘path’)
② If the path is a directory, return a html page with files and directories listed in it.
③ If the path is a file, return the file with a correct mime type. If server cannot decide the MIME type from the file extension (.exe, .mp3, etc.), the MIME type should be application/octet-stream.
(4) If the path doesn’t exist, return an error 404 Not Found.
2. Functions you might need
(1) os.listdir : This can list current directory or a given path.
(2) os.path.isfile : This function can check if a path is a file
(3) os.path.getsize : This function can get the filesize in bytes, which might be useful if you want to add Content-Length to your response header (4) open Example:
file = open(name) writer.write(file.read())
# some server logic except FileNotFoundError:
# write 404 Not Found Response
3. HTML Example
<html><head><title>Index of .//</title></head>
<body bgcolor=”white”>
<h1>Index of .//</h1><hr>
<pre>
<a href=”dir1/”>dir1/</a><br>
<a href=”dir2/”>dir2/</a><br>
<a href=”file1″>file1</a><br>
<a href=”file1″>file1</a><br>
</pre>
<hr>
</body></html>
Reviews
There are no reviews yet.