2015-05-05 25 views
0

我必須在HTML中製作控制按鈕,所以當有人點擊按鈕時,結果應該是Python中的打印函數,用「按鈕被按下」這個詞來表示。 這裏是基本的HTML代碼:在Python中查看HTML的結果

<html> 

<head> 
    <title>Example</title> 
</head> 

<body bgcolor="white"> 
    <form> 
     <input type="button" value="Start" onclick="window.open('http://www.google.cz', 'This is Google')"> 
     <input type="button" value="Color" onclick="document.bgColor='red'"> 
     <input type="button" value="Color_back" onclick="document.bgColor='white'"> 
    </form> 
</body> 

我試圖尋找的與HTML交互的Python代碼一些相關的例子,但這一切,我發現,我真的需要一些幫助。所以,請幫助我:

import SimpleHTTPServer 
import SocketServer 

class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): 
    def do_GET(self): 
     if self.path == '/': 
      self.path = '/Programming for WEB/Robotic_hand_control.html' 
     return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) 

Handler = MyRequestHandler 
server = SocketServer.TCPServer(('localhost', 8080), Handler) 

server.serve_forever() 

回答

0

您需要:

1)在Python代碼另一個處理程序,將接受請求

2)修改的onclick將數據發送到處理程序

3)在處理程序代碼中打印發送的數據

檢查了這一點:https://wiki.python.org/moin/BaseHttpServer

+0

我不知道,如果我知道如何修改的onclick將數據發送到處理程序以及如何打印所發送的數據。我嘗試了上面這樣的東西。 –

0
import BaseHTTPServer 

HOST_NAME = 'localhost' # !!!REMEMBER TO CHANGE THIS!!! 
PORT_NUMBER = 8080 # Maybe set this to 9000. 

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): 
    def do_HEAD(s): 
     s.send_response(200) 
     s.send_header("Content-type", "text/html") 
     s.end_headers() 

    def do_GET(s): 
     """Respond to a GET request.""" 
     s.send_response(200) 
     s.send_header("Content-type", "text/html") 
     s.end_headers() 
     s.wfile.write("<html><head><title>Example</title>") 
     s.wfile.write("<script type=\"text/javascript\"> function myFunction(){document.getElementById(\"demo\").innerHTML=\"Hello World\";}</script></head>") 
     s.wfile.write("<body> <button onclick = \"myFunction()\">Click to print</button> <p id=\"demo\"></p>") 
     # If someone went to "http://something.somewhere.net/foo/bar/", 
     # then s.path equals "/foo/bar/". 
     s.path = '/Programming for WEB - CTU/Primjer2.php' 
     s.wfile.write("</form></body></html>") 

if __name__ == '__main__': 
    server_class = BaseHTTPServer.HTTPServer 
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler) 
    print MyHandler.do_GET(s.wfile.write(myFunction())) 

但這不起作用。

這裏是HTML代碼:

<head> 
    <title>Example</title> 
    <script type="text/javascript"> 
     function myFunction(){ 
      document.getElementById("demo").innerHTML="Hello World"; 
     } 
    </script> 
</head> 

<body> 
    <button onclick = "myFunction()">Click to print</button> 
    <p id="demo"></p> 
</body>