{{docContent.title}}

简单httpserver

废话不多少,直接上代码

host = ('', 8888)
class Resquest(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()
        #获取路径
        print(self.path)
        # 获取温度数据
        data={}
        data["temp"]=48
        data["Humidity"]=78
        data["smoke"]="dangerous"
        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()