我正在设置我的第一个存储POST请求的HTTP服务器。处理程序类BaseHTTPRequestHandler有问题。
在我的方法中,我想将服务器希望响应客户端的响应代码传递给处理程序(自动测试目的)。我在这里找到了很多帮助,我将在下面分享与我开发的子类有关的代码,但是由于我不知道从哪里获取参数,我仍然需要弄清楚如何初始化我的处理程序: “请求”,“ client_address”和“服务器”。
我在各自的位置上放置了三个问号。
感谢您的帮助,对不起您的解释不好。
class ServerHandler(BaseHTTPRequestHandler):
def __init__(self, response_code, request, client_address, server):
super().__init__(request, client_address, server)
self.response_code = response_code
def _set_header(self):
self.send_response(self.response_code)
self.send_header('content-type', 'text/html')
self.end_headers()
def do_HEAD(self):
self._set_header()
def do_POST(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST'}
)
# get POST data
utc = int(form.getvalue("utc"))
value = form.getvalue("value")
sn = form.getvalue("sn")
# manage data
dataManager(utc, sn, value)
# send response
self._set_header()
response = "<RESULT>0</RESULT>"
self.wfile.write(response.encode('utf-8'))
class Server:
def __init__(self, port, response_code=200, server_class=HTTPServer):
self.port = port
self.response_code = response_code
self.server_address = ('', self.port)
handler_class = ServerHandler(self.response_code, ?, ?, ?)
self.server = server_class(self.server_address, handler_class)
self.server_thread = threading.Thread(target=self.server.serve_forever)
self.server_thread.daemon = True
def run(self):
self.server_thread.start()
print("Server started")
def shutdown(self):
self.server.shutdown()
self.server.server_close()
print("Server stopped")
最佳答案
我解决了以下问题。我没有将response_code作为函数参数传递,而是添加了response_code作为类变量,并从我直接在公共类变量response_code上编写的类Server中添加了它。而已。不是最好的解决方案,但它可以工作。如果您有更好的主意,请告诉我。
class ServerHandler(BaseHTTPRequestHandler):
def __init__(self, request, client_address, server):
super().__init__(request, client_address, server)
self.response_code = response_code
def _set_header(self):
self.send_response(self.response_code)
self.send_header('content-type', 'text/html')
self.end_headers()
def do_HEAD(self):
self._set_header()
def do_POST(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST'}
)
# get POST data
utc = int(form.getvalue("utc"))
value = form.getvalue("value")
sn = form.getvalue("sn")
# manage data
dataManager(utc, sn, value)
# send response
self._set_header()
response = "<RESULT>0</RESULT>"
self.wfile.write(response.encode('utf-8'))
class Server:
def __init__(self, port, response_code=200, server_class=HTTPServer):
self.port = port
self.server_address = ('', self.port)
handler_class = ServerHandler
handler_class.response_code = response_code
self.server = server_class(self.server_address, handler_class)
self.server_thread = threading.Thread(target=self.server.serve_forever)
self.server_thread.daemon = True
def run(self):
self.server_thread.start()
print("Server started")
def shutdown(self):
self.server.shutdown()
self.server.server_close()
print("Server stopped")
关于python - 重写BaseHTTPRequestHandlerClass,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57824330/