python的json接受_如何接受JSON作为pythonTornado“post”方法的输入

本文档描述了如何在Tornado web应用中处理JSON POST请求。作者试图从JSON输入中获取浮点数并更新传感器设定点参数。Get方法工作正常,但Post方法遇到问题,无法正确解析和处理JSON数据。代码示例展示了尝试解析JSON和调用外部函数的片段,但目前只能返回'Hello World'。
摘要由CSDN通过智能技术生成

我尝试使用tornado来做一个简单的get和post方法。tornado框架相当新。对于我想接受json作为输入的post,使用该输入输入到另一个函数中,我必须执行另一部分代码。但是我不能让龙卷风后的方法工作,即使是一个简单的自写(). 在

对于get方法,我从SQL数据库中读取以获取传感器的状态,并将其写入json格式。get方法非常有效!当我去的时候本地主机:端口它读取我的get json文件。对于post方法,我希望接受一个只有一个的简单json关键:值哪个是一个浮点数。我想获取用户在json中指定的浮点数,并在我的flowMKS.set()改变传感器设定点参数的功能。我不知道如何将json输入post方法并将其读入变量。我在下面有一些注释过的代码,我尝试过了,但没有成功。但是我回到了最基本的地方自写(“Hello World”)查看帖子是否有效。我不能得到自写也可以工作。当我转到时一直收到500条错误消息本地主机:端口#/flow_post。变量flow_status在我的get方法中使用。在

预期的结果是在post方法中引入一个json{“setpoint”:45.5}。使用数字并插入到我的flowMKS方法来更改传感器上的参数。在

如何将json引入post方法,并从json输入中获取数字并存储在变量中?在class Current(tornado.web.RequestHandler):

def get(self):

global flow_status

time = flow_status[0]

ip = flow_status[1]

rate = flow_status[2]

setp = flow_status[3]

tempc = flow_status[4]

status = {"flow_controller":{

"time":time,

"ip":ip,

"rate_sccm":rate,

"setpoint":setp,

"temperature_c":tempc,

}

}

self.write(status)

class Update(tornado.web.RequestHandler):

# def prepare(self):

# if self.request.haders["Content-Type"].startswith("application/json"):

# self.json_args = json.loads(self.request.body)

# else:

# self.json_args = None

def post(self):

# #expecting body data to contain JSON so we use json.loads to decrypt the JSON into a dict

# data = json.loads(self.request.body)

#

# #Getting what the setpoint should be

# setpoint = self.json_args["setpoint"]

#

# #making the input a float

# setpoint = float(setpoint)

#

# #setting up connection with sensor

# flowMKS = FlowController(flow_status[1])

#

# #sending setpoint to sensor

# flowMKS.set(setpoint)

self.write("Hello World")

if __name__ == '__main__':

# global flow_status

#Below is creating the Tornado based API for get and post methods

tornado.options.parse_command_line()

app = tornado.web.Application(

handlers=[(r'/',Current), (r'/flow_post', Update)])

http_server = tornado.httpserver.HTTPServer(app)

http_server.listen(options.port)

#using PeriodicCallback to get info from the SQL database every 500 ms

PeriodicCallback(get_sql_status,500).start()

#starting the entire Tornado IOLoop

tornado.ioloop.IOLoop.current().start()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值