2017-05-15 7 views
0

BaseHTTPRequestHandler 모듈을 사용하여 파이썬에서 POST 요청을 받으려고합니다.HTML 폼 (파이썬)에서 빈 POST 요청

<html> 
 
    <head> 
 
     <meta charset='UTF-8'> 
 
    </head> 
 
    <body> 
 
     <form action="http://localhost:8080/" method='post'> 
 
     <strong>Access point</strong><br> 
 
     <label for='ssid'>SSID: </label> 
 
      <input type='text' id=ssid><br> 
 
     <label for='passphrase'>Passphrase: </label> 
 
      <input type='password' id=passphrase><br> 
 
     <br> 
 
     <strong>Calendar</strong><br> 
 
     <label for='id'>ID: </label> 
 
      <input type='text' id=calid><br> 
 
     <br> 
 
     <input type='submit'> 
 
     <input type='reset'> 
 
     </form> 
 
    </body> 
 
    <html>

과 최소한의 응용 프로그램 :

# -*- coding: utf-8 -*- 

from http.server import BaseHTTPRequestHandler, HTTPServer 
import socketserver 
import socket 
import sys 

class ConfigHTTPRequestHandler(BaseHTTPRequestHandler): 
    def _set_headers(self): 
     self.send_response(200) 
     self.send_header('Content-type', 'text/html') 
     self.end_headers() 

    def do_GET(self): 
     self._set_headers() 
     with open("index.html", "rb") as f: 
      self.wfile.write(f.read()) 

    def do_HEAD(self): 
     self._set_headers() 

    def do_POST(self): 
     print(self.headers) 

     content_length = int(self.headers.get('Content-Length', 0)) 
     config_string = self.rfile.read(content_length).decode("UTF-8") 
     print("Content length: ", content_length) 
     print("Config string: [ ", config_string, " ]") 

     self._set_headers() 
     return 

ConfigHTTPRequestHandler.protocol_version = "HTTP/1.0" 
httpd = HTTPServer(("127.0.0.1", 8080), ConfigHTTPRequestHandler) 

sa = httpd.socket.getsockname() 
print("Serving HTTP on", sa[0], "port", sa[1], "...") 
try: 
    httpd.serve_forever() 
except KeyboardInterrupt: 
    print("\nKeyboard interrupt received, exiting.") 
    httpd.server_close() 
    sys.exit(0) 

문제는 모든 POST 요청이 비어 있다는 것입니다 나는 간단한 HTML 양식을 썼다. content_length = 0이됩니다.

답변

2

속성을 양식 요소에 제공하고 작동하는지 확인하십시오.

<html> 
    <head> 
     <meta charset='UTF-8'> 
    </head> 
    <body> 
     <form action="http://localhost:8080/" method='post'> 
      <strong>Access point</strong><br> 
      <label for='ssid'>SSID: </label> 
      <input type='text' id="ssid" name="ssid"><br> 
      <label for='passphrase'>Passphrase: </label> 
      <input type='password' id="passphrase" name="passphrase"><br> 
      <br> 
      <strong>Calendar</strong><br> 
      <label for='id'>ID: </label> 
      <input type='text' id="calid" name="calid"><br> 
      <br> 
      <input type='submit'> 
      <input type='reset'> 
     </form> 
    </body> 
<html> 
+0

고맙습니다. – DBLouis