# encoding: utf-8 # @author: 原凯峰 # @contact: 2894340009@qq.com # @software: pycharm # @file: Server.py # @time: 2024/6/19 9:22 # @desc: import json from http.server import BaseHTTPRequestHandler, HTTPServer from MessageHandler.PreDataProcessor import * import time class CORSHTTPRequestHandler(BaseHTTPRequestHandler): def do_OPTIONS(self): self.send_response(204) self.send_header('Content-type', 'text/plain') self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS') self.send_header('Access-Control-Allow-Headers', 'Content-Type') self.end_headers() def do_POST(self): content_length = int(self.headers['Content-Length']) post_data = self.rfile.read(content_length) self.send_response(200) self.send_header('Content-type', 'text/plain') self.send_header('Access-Control-Allow-Origin', '*') self.end_headers() print("Request line:", self.requestline) print("Headers:", self.headers) print("Body:", post_data.decode('utf-8')) # 处理程序 MessageProcessor = preDataProcessor(post_data) # response = {"POST": "request received"} # self.wfile.write(str(response).encode('utf-8')) # time.sleep(1) backMessage =MessageProcessor.returnMessage response = {"POST": backMessage} self.wfile.write(str(json.dumps(response)).encode('utf-8')) # self.wfile.write("{\"POST\": \"request received\"}".encode('utf-8')) def do_GET(self): self.send_response(200) self.send_header('Content-type', 'text/plain') self.send_header('Access-Control-Allow-Origin', '*') self.end_headers() self.wfile.write(b"Hello, world!")