Python Forum
Basic client server code question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: Basic client server code question (/thread-31409.html)



Basic client server code question - swisscheese - Dec-09-2020

# 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.")



RE: Basic client server code question - Larz60+ - Dec-09-2020

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.


RE: Basic client server code question - swisscheese - Dec-11-2020

It's not a homework assignment. I sounds that way because I stripped the code down to a trivial case for clarity.


RE: Basic client server code question - ndc85430 - Dec-12-2020

Why are you writing your own web framework instead of using an existing one (like Flask, but others are available)?


RE: Basic client server code question - Larz60+ - Dec-12-2020

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-flask-mega-tutorial-part-i-hello-world