from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import threading
class Handler(BaseHTTPRequestHandler):
done = False
def do_POST(self):
data = json.loads(self.rfile.read(int(self.headers['Content-Length'])))
self.send_response(200)
self.end_headers()
if data.get('status') == 'success': # 预期条件
self.wfile.write(b'符合预期,服务器停止')
Handler.done = True
else:
self.wfile.write(b'继续监听')
def run():
server = HTTPServer(('', 8080), Handler)
threading.Thread(target=server.serve_forever, daemon=True).start()
while not Handler.done: pass
server.shutdown()
print('服务器已停止')
if __name__ == '__main__':
run()