from http.server import HTTPServer, BaseHTTPRequestHandler import json data = {'result': 'this is a test'} host = ('localhost', 8888) class Resquest(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write(json.dumps(data).encode()) if __name__ == '__main__': server = HTTPServer(host, Resquest) print("Starting server, listen at: %s:%s" % host) server.serve_forever()
啓動服務,在控制檯看到:json
在瀏覽器輸入http://localhost:8888/進行訪問:瀏覽器
此時查看控制檯能看到log已經打印了請求:app