如何序列化python-flask变量



来自烧瓶进口烧瓶从烧瓶导入render_template、重定向、请求从flask_restful导入资源,Api导入请求进口泡菜

app=烧瓶(名称(api=api(应用程序(

类HelloWorld(资源(:def post(self(:r=请求.get('https://samples.openweathermap.org/data/2.5/weather?zip=16802,美国&appid=157d320b7d029e653c67902f982784f'(

json_object_r = r.json()
temp_k = float(json_object_r['main']['temp'])
temp_c = temp_k - 273.15
tempp_c = int(temp_c)
pickle_in = open("var.pickle", "wb")
tempp_c = pickle.load(pickle_in)

# pressure
p = requests.get('https://samples.openweathermap.org/data/2.5/weather?zip=16802,us&appid=157d320b7d029e653c67902f982784ff')
json_object_p = p.json()
press_k = float(json_object_p['main']['pressure'])
# wind
# speed
w = requests.get('https://samples.openweathermap.org/data/2.5/weather?zip=16802,us&appid=157d320b7d029e653c67902f982784ff')
json_object_w = w.json()
wind_k = float(json_object_w['wind']['speed'])
# gust
g = requests.get('https://samples.openweathermap.org/data/2.5/weather?zip=16802,us&appid=157d320b7d029e653c67902f982784ff')
json_object_g = g.json()
gust_g = float(json_object_g['wind']['gust'])

return {tempp_c}

api.add_resource(HelloWorld,'/'(

如果name='main':app.run(debug=True(

创建一个首先发送串行对象的程序:

import flask
import pickle
object_to_send = {1:2}
@app.route('/endpoint')
def endpoint():
return pickle.dumps(object_to_send).hex()
app.run(port=8080)

然后你可以从另一个python程序调用它,并读取该对象:

import pickle
import requests
object_hex = requests.get('http://127.0.0.1:8080/endpoint').text
object_received = pickle.loads(bytearray.fromhex(object_hex))
print(object_received)

这绝对不是一个最佳实践——如果可能的话,也许你应该串行化为json,或者使用一些消息传递框架,它应该支持不同的串行化方法,或者像Kafka这样的消息总线。

相关内容

  • 没有找到相关文章

最新更新