python3 使用http.server模塊 搭建一個簡易的http服務器
阿新 • • 發佈:2018-01-19
test class ont elf resp his local and height
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()
啟動服務,在控制臺看到:
在瀏覽器輸入http://localhost:8888/進行訪問:
此時查看控制臺能看到log已經打印了請求:
python3 使用http.server模塊 搭建一個簡易的http服務器