来自 HTML 表单的空 POST 请求 (Python)
Empty POST request from HTML form (Python)
我正在尝试使用 BaseHTTPRequestHandler 模块在 Python 中接收 POST 请求。我写了一个简单的HTML形式:
<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 请求都是空的。我得到 content_length = 0.
请为您的表单元素提供 name 属性,看看它是否有效。
<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>
我想详细说明user7883492的回答。
根据 this protocol description 仅提交了 "successful controls" 表格。
根据 this 一个成功的控件需要有一个名称属性。
除了 name 属性要求外,您的控件满足了被视为 "successful control" 的所有要求。
这就是他们没有提交的原因。
添加名称属性后,它们变为 "successful controls",并已提交。
我正在尝试使用 BaseHTTPRequestHandler 模块在 Python 中接收 POST 请求。我写了一个简单的HTML形式:
<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 请求都是空的。我得到 content_length = 0.
请为您的表单元素提供 name 属性,看看它是否有效。
<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>
我想详细说明user7883492的回答。
根据 this protocol description 仅提交了 "successful controls" 表格。
根据 this 一个成功的控件需要有一个名称属性。
除了 name 属性要求外,您的控件满足了被视为 "successful control" 的所有要求。
这就是他们没有提交的原因。
添加名称属性后,它们变为 "successful controls",并已提交。