简体   繁体   中英

PYTHON: no response in html

so im trying to create a simple page were after clicking the "submit" button i get the response "it works.".,(it's something very simple in order for me to get somewhere bigger. to get used to this part of coding).. the only thing is that i dont get the response in html.? any advise?

**server: **



# get the response in html thru request

# http_local_server_test.py

import http.server
from http.server import BaseHTTPRequestHandler, HTTPServer
from logging import Handler
import requests
import socketserver
import json

\#json_str = json.dumps({'name': 'Alice', 'age':29})

class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):

        #if self.path =='/':
            #self.path = '/test.html'
        
        try:
            file_to_open = open(self.path[1:]).read()
            self.send_response(200)
            
        #this code execute when a GET request happens
            if self.path.find("isButtonPressed=true") != -1:
                print("Button clicked")
                
            #the result => ? return super().do_GET()
            
                return self.wfile.write(bytes("<p>It works!.</p>", "utf-8"))
                #self.send_header('Content-Type', 'application/json')
                #return self.wfile.write(json_str.encode(encoding='utf_8'))
        except:
            file_to_open = "File not found"
            self.send_response(404)
        self.end_headers()
        #self.wfile.write(bytes(file_to_open, 'utf-8'))

PORT = 8080
myHandler = Handler

with socketserver.TCPServer(("", PORT), myHandler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()

***client:



\<!DOCTYPE html\>
\<html lang="en"\>
\<head\>
\<meta charset="UTF-8"\>
\<title\> INDEX TEST \</title\>
\</head\>

    <body>
        <!--<button onclick="">Get</button>-->
        <!-- CLIENT SIDE-->
        <!-- Get button -->
        <p> Text text text </p>
        <form action="/test.html">
            <input type="hidden" name="isButtonPressed" value="true">
            <input type="submit">
    </body>

\</html\>


Use Flask, you'll have a better time with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM