gpt4 book ai didi

python - 重写 BaseHTTPRequestHandlerClass

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:56 27 4
gpt4 key购买 nike

我正在设置第一个存储 POST 请求的 HTTP 服务器。我的处理程序类 BaseHTTPRequestHandler 有问题。在我的方法中,我想将响应代码传递给处理程序,我希望服务器响应客户端(自动测试目的)。我在这里找到了很多帮助,我将分享下面的代码来了解我开发的子类的相关内容,但我仍然需要弄清楚如何初始化我的处理程序,因为我不知道从哪里获取参数: “请求”、“客户端地址”和“服务器”。我在各自的位置打了三个问号。感谢您的帮助,并对错误的解释表示歉意。

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添加为类变量,并从类Server中直接在公共(public)类变量response_code上写入。就是这样。不是最好的解决方案,但它有效。如果您有更好的想法请告诉我。

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com