fromhttp.serverimportHTTPServer,BaseHTTPRequestHandlerimportjsonimporttracebackimportthreadingimporttimetemperature_dict={"max":[6,1,1,7,3,8,5],"min":[-2,-6,-6,-1,-4,-5,-6],}port=12345process_duration=0.2classResquestHandler(BaseHTTPRequestHandler):def_ResponseJsonDict(self,status_code:int,json_dict:dict)->None:self.send_response(code=status_code)self.send_header("Content-type","application/json")self.end_headers()response_json=json.dumps(json_dict)self.wfile.write(response_json.encode())returndefdo_GET(self):try:time.sleep(process_duration)self._ResponseJsonDict(status_code=200,json_dict=temperature_dict)exceptExceptionaserr:error_message=f"\n{err}{type(err)}\n{traceback.format_exc()}\n"print("error",error_message)response_dict={"message":f"{error_message}\nContact the developer team...\n"}self._ResponseJsonDict(status_code=503,json_dict=response_dict)defThreadHttpServer():"""提供 HTTP 服务的线程"""print("log",f"开始在{port}端口提供HTTP服务")# `0.0.0.0` means that this server is exposed to all network interfaceshost=("0.0.0.0",port,)server=HTTPServer(server_address=host,RequestHandlerClass=ResquestHandler)server.serve_forever()if__name__=="__main__":th_HttpServer=threading.Thread(target=ThreadHttpServer,name=None,args=[])th_HttpServer.daemon=Trueth_HttpServer.start()whileTrue:userInput=input()ifuserInput=="q":exit(0)
Firebase is a Backend-as-a-Service (BaaS) app development platform that provides hosted backend services such as a realtime database, cloud storage, authentication, crash reporting, machine learning, remote configuration, and hosting for your static files. -- Flutter Data & backend | Firebase