1. 程式人生 > >python3 使用http.server模塊 搭建一個簡易的http服務器

python3 使用http.server模塊 搭建一個簡易的http服務器

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服務器