Python Forum
Basic client server code question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic client server code question
#1
# 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.")
Reply
#2
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.
Reply
#3
It's not a homework assignment. I sounds that way because I stripped the code down to a trivial case for clarity.
Reply
#4
Why are you writing your own web framework instead of using an existing one (like Flask, but others are available)?
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,276 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Client/Server proper finalizing transfer wolfman5874 1 1,422 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,361 Jun-12-2022, 05:57 PM
Last Post: gpropf
  Server/client basic communication ebolisa 0 2,009 Sep-30-2021, 12:22 PM
Last Post: ebolisa
  Client server Multithreading Anan 6 5,754 Apr-21-2021, 08:19 PM
Last Post: SheeppOSU
Question Trouble with Client/Server reverse Shell! Gilush 0 2,757 Feb-03-2021, 01:04 PM
Last Post: Gilush
  How can i create a server for already existing client using Python? Chapanson 21 7,316 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Simple TCP Client and TCP Server Problem Vapulabis 5 4,335 Jul-12-2020, 05:09 PM
Last Post: ndc85430
  how to send an image from server to client using PICKLE module dafdaf 1 3,066 Jun-02-2020, 01:08 PM
Last Post: nuffink
  how can i send a list of tuples from the server to the client using sockets? dafdaf 1 3,830 Apr-13-2020, 10:51 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020