You can create a simple HTTP server and parse cookies in Python by using the built-in http.server module. Here's an example:
main.py699 chars22 linesIn this code, we define a custom MyHTTPRequestHandler class which extends BaseHTTPRequestHandler. It overrides the do_GET() method, which is called for every GET request received by the server. Inside do_GET(), we:
Cookie header using the SimpleCookie class from the http.cookies module.name cookie, or use 'anonymous' if the cookie is not present.To run this server, save the code in a file named server.py, and execute it with Python:
main.py19 chars2 lines
Then open a browser and go to http://localhost:8000. You should see a greeting message with your name, if you have a name cookie set. If you don't have a name cookie set, the message will say "Hello, anonymous!".
gistlibby LogSnag