为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 Bottle 在 python 中获取由 nodeJS 服务器发送的请求数据

如何使用 Bottle 在 python 中获取由 nodeJS 服务器发送的请求数据

MMTTMM 2022-08-16 18:59:17
如何在python中获取和打印nodeJS发送的数据?我在这个节点Js模块中使用ExpressJSapp.use("/py/sendomodel",  function (req, res, next) {     var oData = {        "Test":"FirstData"     }    var options = {        method: 'POST',        data : oData,        url: 'https://xxx.cfapps.us10.hana.ondemand.com/mprs/omodel',        headers: {            'cache-control': 'no-cache',            /*'Content-Type' :'application/json',*/            Connection: 'keep-alive',            'accept-encoding': 'gzip, deflate',            Host: 'xxxx.cfapps.us10.hana.ondemand.com',            'Cache-Control': 'no-cache',            Accept: '*/*',            'User-Agent': 'PostmanRuntime/7.15.0'        }    };    return request(options, function (error, response,body,data) {        if (error) throw new Error(error);    });});现在我被困在这里,如何打印发送的数据?这是python模块from bottle import route, run, post, request, response@route('/mprs/omodel', method='POST')def profile():    #I tried all these without any success , I want to print the oData that I have sent via nodeJs    #request.body.read().decode('utf8')    temp = request.body.read()    #temp = request.json    #sol = request.forms    print(temp)       #jsonData = json.load(request.body)    #return jsonData    return(temp)
查看完整描述

1 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

您有两件独立的事情要看,首先是查询,其次是表单数据。为了以防万一,我将两者合并。在您的示例中,正文为空。由于没有实际的HTML。


from bottle import route, run, post, request, response


def merge_dicts(*args):

    result = {}

    for dictionary in args:

        result.update(dictionary)

    return result


@post('/mprs/omodel')

def profile():

    payload = merge_dicts(dict(request.forms), dict(request.query.decode()))

    print(payload)

    print(payload['Test'])

    return payload


查看完整回答
反对 回复 2022-08-16
  • 1 回答
  • 0 关注
  • 158 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号