Python Forum

Full Version: Basic client server code question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
# This is a simple python web server which displays a button and “Hello”.
# How can I modify the code so that when I click the button the webpage shows the word “There”?
# I need the button to make a request to the server which will then serve the HTML with the change.
# I’ve been searching for days for an answer to this.
# My project does more than this but this is all I need to move forward.
from http.server import BaseHTTPRequestHandler, HTTPServer
Template = """<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head><body>Hello <button class="button">Click</button></body></html>"""

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(bytes(Template, "utf-8"))

if __name__ == "__main__":        
    webServer = HTTPServer(("localhost",80), MyServer)
    print("Server started http://%s:%s" % (hostName, serverPort))
    try: webServer.serve_forever()
    except KeyboardInterrupt: pass
    webServer.server_close()
    print("Server stopped.")
This looks like a very elementary code (html) question, part of a homework assignment.
Is that the case?
The author of this code, should know the answer.
It's not a homework assignment. I sounds that way because I stripped the code down to a trivial case for clarity.
Why are you writing your own web framework instead of using an existing one (like Flask, but others are available)?
As suggested, this can be done quite easily with flask.

Learning enough flask will probably take several hours.

see the following tutorial: https://blog.miguelgrinberg.com/post/the...ello-world