spacebar
to navigate to the next slide.s
for speaker notes.Authoritative wording will be made available this week.
We can subtract points for bad code style and practices. You have one week, from the assignment evaluation, to fix the issues and submit fixed solution.
In case of any problem (e.g. illness) notify the teacher immediately!
Cooperation, during labs, is allowed, but do not disturb others.
Log in to a website (e.g. SIS). Open developer tools using on of the following methods (Chrome, Edge, FireFox):
Explore and comment on following tabs:
Inspired by Python tutorial. This is NOT a production code!
#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
HOST = "localhost"
PORT = 8080
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html; charset=utf-8")
self.end_headers()
self.wfile.write(bytes("<!doctype html><title>My Page</title>", "utf-8"))
if __name__ == "__main__":
server = HTTPServer((HOST, PORT), MyServer)
try:
server.serve_forever()
except KeyboardInterrupt:
pass
server.server_close()
What is missing in the code?
We need a way to access the information about the request, like the header or path. This information is available using self in the handler method.
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html; charset=utf-8")
self.end_headers()
self.wfile.write(bytes("<!doctype html><title>My Page</title>", "utf-8"))
# The next line is new!
self.wfile.write(bytes(f"<p>Request: {self.path} </p>" , "utf-8"))
Different frameworks employ different ways to access the request information and produce the response.
Use Python to server the content.html file you've created. You can build on the given Python example. At the end you should be able to access http://localhost:8080/, or http://localhost:8080/content.html, and see the website in your browser.
Read values from URL query and substitute them into the file.
For example query: ..?name=Ailish&age=19 and template:
<p>{name} is {age} years old.</p>
Should produce output:
<p>Alilis is 19 years old.</p>
Run following command to server content of current directory.
python3 -m http.server
This will start a simple webserver, serving files in the directory.
Change the GET method.
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html; charset=utf-8")
self.end_headers()
with open("content.html", "rb") as stream:
self.wfile.write(stream.read())
Content of public_html is available at https://webik.ms.mff.cuni.cz/~<username>/
.
When connecting to webik, keep in mind that it has limited resources.
ssh -p 42222 <username>@webik.ms.mff.cuni.cz
scp -P42222 index.html <username>@webik.ms.mff.cuni.cz:public_html/
Host webik
HostName webik.ms.mff.cuni.cz
User <username>
Port 42222
Now you can use just: ssh webik
Both should be installed on lab machines. We recommend to familiarize yourself with the command line alternatives.
https://webik.ms.mff.cuni.cz/~<username>/
.