http.server
can only handle one request at a time, therefore the bookmarker server cannot fetch a page from itselfAdd:
import threading
from socketserver import ThreadingMixIn
class ThreadHTTPServer(ThreadingMixIn, http.server.HTTPServer):
"This is an HTTPServer that supports thread-based concurrency."
Modify:
if __name__ == '__main__':
port = int(os.environ.get('PORT', 8000))
server_address = ('', port)
httpd = ThreadHTTPServer(server_address, Shortener)
httpd.serve_forever()
Specialized web server programs — like Apache, Nginx, or IIS can serve static content from disk storage very quickly and efficiently. They can also provide access control, allowing only authenticated users to download particular static content.
A way that a server can ask a browser to retain a piece of information, and send it back to the server when the browser makes subsequent requests
from http.cookies import SimpleCookie, CookieError
out_cookie = SimpleCookie()
out_cookie["bearname"] = "Smokey Bear"
out_cookie["bearname"]["max-age"] = 600
out_cookie["bearname"]["httponly"] = True
self.send_header("Set-Cookie", out_cookie["bearname"].OutputString())
Create a SimpleCookie
from the Cookie
header
in_cookie = SimpleCookie(self.headers["Cookie"])
in_data = in_cookie["bearname"].value
Cookie
header will raise a KeyError
exceptionSimpleCookie
constructor will raise http.cookies.CookieError
.For a lot more information on cookie handling in Python, see the documentation for the http.cookies
module.
The starter code for this exercise is in Lesson-3/2_CookieServer
.
HTTPS encryption follows a standard protocol called Transport Layer Security (TLS)
The data in the TLS certificate and the server’s private key are mathematically related to each other through a system called public-key cryptography
When the browser connects to a particular server, if the TLS domain metadata doesn’t match the DNS domain, the browser will reject the certificate and put up a big scary warning to tell the user that something fishy is going on.
Every request and response sent over a TLS connection is sent with a message authentication code (MAC) that the other end of the connection can verify to make sure that the message hasn’t been altered or damaged in transit.
PUT
for creating resourcesThe HTTP PUT
method can be used for creating a new resources. The client sends the URI path that it wants to create, and a piece of data in the request body.
A server should respond to a PUT
request with a 201 Created
status code, if the PUT action completed successfully. After a successful PUT
, a GET
request to the same URI should return the newly created resource.
DELETE
for deleting thingsAfter a DELETE
has happened successfully, further GET
requests for that resource will yield 404 Not Found
One standardized format for PATCH
requests is the JSON Patch format, which expresses changes to a piece of JSON data. A different one is JSON Merge Patch.
HEAD
, OPTIONS
, TRACE
for debuggingHEAD
works just like GET
, except the server doesn’t return any content — just headers.OPTIONS
can be used to find out what features the server supports.TRACE
echoes back what the server received from the client — but is often disabled for security reasons.You can read much more about HTTP/2 in the HTTP/2 FAQ.
Lesson-3/3_Parallelometer
The browser can send several requests all at once, and the server can send responses as quickly as it can get to them. There’s no limit on how many can be in flight at once.
Server push allows the server to say, effectively, “If you’re asking for index.html
, I know you’re going to ask for style.css
too, so I’m going to send it along as well.”