Status on 2021/01/19, 08:58
Silly game of dice that you can play in your browser or over the network (tis just a simple python WSGI application):
#!/usr/bin/env python3 # # Single-file dice game over the network # import wsgiref.simple_server import random PORT = 8000 def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8')]) my_number = random.randint(1, 6) your_number = random.randint(1, 6) if my_number > your_number: result = "You lose!" elif my_number == your_number: result = "It's a draw." else: result = "Ack, you win!" response = '''<!DOCTYPE html> <html> <head> <title>Roll the dice</title> </head> <body> <p> Your number was: %s </p> <p> My number was: %s </p> <p> <strong>%s</strong> </p> <p> To play again, just refresh the page. </p> </body> </html> ''' % (your_number, my_number, result) return [response.encode('utf-8')] server = wsgiref.simple_server.make_server('', PORT, application) print("Now listening on port %s..." % PORT) server.serve_forever()
Do I recommend this to anyone wanting to build a proper, scalable web application? Likely not, but it's good to know that Python has the capability to do this kind of thing already built-in.