从Bottle Server访问HTTP POST数据

时间:2015-05-18 23:18:08

标签: python json http post bottle

我有一个简单的python http监听服务器,如下所示。

This class prints out a complex nested dictionary with sub dictionaries and sub lists.  
##
## Recursive class to parse and print complex nested dictionary
##

class NestedDictionary(object):
    def __init__(self,value):
        self.value=value

    def print(self,depth):
        spacer="--------------------"
        if type(self.value)==type(dict()):
            for kk, vv in self.value.items():
                if (type(vv)==type(dict())):
                    print(spacer[:depth],kk)
                    vvv=(NestedDictionary(vv))
                    depth=depth+3
                    vvv.print(depth)
                    depth=depth-3
                else:
                    if (type(vv)==type(list())):
                        for i in vv:
                            vvv=(NestedDictionary(i))
                            depth=depth+3
                            vvv.print(depth)
                            depth=depth-3
                    else:
                        print(spacer[:depth],kk,vv) 

##
## Instatiate and execute - this prints complex nested dictionaries
## with sub dictionaries and sub lists
## 'something' is a complex nested dictionary

MyNest=NestedDictionary(weather_com_result)
MyNest.print(0)

如果我从另一台服务器执行某些数据(JSON文件)的POST,如下所示

from bottle import route, run

@route('/',method='POST')
def default():
    return 'My first bottle program.'

run(host='192.168.132.125', port=1729)

我得到了输出

curl -X POST -d @data_100 http://192.168.132.125:1729/

现在我希望我的Bottle服务器将发布的JSON文件的内容转储到服务器上的文件夹中。如何实现这一点。

2 个答案:

答案 0 :(得分:2)

您可能需要查看Bottle built-in json property

没有错误检查,它看起来像这样:

@route('/', method='POST')
def default():
    json_text = request.json
    with open('/path/to/file', 'wb') as f:
        f.write(json_text)
    return 'My first bottle program.'

答案 1 :(得分:0)

您可以使用request.forms对象访问已发布的表单数据。 然后用标准的python工具解析/ dump / do_anything。

您可以在此处阅读有关FormDict及其方法的信息: http://bottlepy.org/docs/dev/api.html#bottle.FormsDict