简体   繁体   中英

python to print item in list from a json post

I'm running Python 3.7 Flask RestFul I have data being post in json format to my api Flask app. the data i receive looks like the below:

{'username': 'admin', 'password': 'mypassword', 'host': [{'address': '192.168.2.5', 'config': 'set system host-name device01', 'address': '192.168.2.2', 'config': 'set system host-name device02'}]}

the List 'host': can be up to several hundred devices / ip address

I need to parse thru the list and print out only the IP address and Config so I need to get it to be:

192.168.2.5
192.168.2.2

and so on.... so I can loop thru the IP address list and connect to the device and apply the config that will be different per device.

The Structure I would need in return so I can loop thru and connect to device IP address then apply the config would be...

192.168.2.5
set system host-name device01

192.168.2.2
set system host-name device02

were the ip address would = ips and the config would = config

my python code is below...

app = Flask(__name__)
api = Api(app)

class build(Resource):

def post(self):

    data = request.get_json(force=True)
    print(data)
    jobname = data['jobname']
    username = data['username']
    password = data['password']
    configs = []
    ips = []
    for x in data['host']:
        if 'address' in x:
            ips.append(x['address'])
        elif 'config' in x:
            configs.append(x['config'])
    commands = zip(ips, configs)
    for command in commands:
        iplist = '{}'.format(command[0])
        print(iplist)
        conf = '{}'.format(command[1])
        print(conf)

        # Connect to Device and load config
        dev = Device(host=iplist, user=username, passwd=password)
        dev.open()
        print("connect to %s " % iplist)
        dev.timeout = 600
        #print(dev.cli("show version"))
        dev.bind(cfg=Config)
        dev.cfg.load(conf, format='set', merge=True)
        dev.cfg.commit()
        dev.close()

api.add_resource(build, '/build')

if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)

This assumes that the address and configs are in order. Whoever is giving you the data or outputting the data could have merged the address and config dicts making this much safer and simpler.

import gevent

data = request.get_json(force=True)

def execute(username, password, command):
    command = command[0]
    dev = Device(host=command, user=username, passwd=password)
    dev.open()
    print("connect to %s " % command)
    dev.timeout = 600
    print(dev.cli("show version"))
    dev.bind(cfg=Config)
    dev.cfg.load(command, format='set', merge=True)
    dev.cfg.commit()
    dev.close()

commands = []
data = {'username': 'admin', 'password': 'mypassword', 'host': [{'address': '192.168.2.5','config': 'set system host-name device01'}, {'address': '192.168.2.2','config': 'set system host-name device02'}]}
username = data['username']
password = data['password']

for x in data['host']:
    if 'address' in x:
        commands.append((x['address'],x['config']))

threads = [gevent.spawn(execute, username, password, command) for command in commands]
gevent.joinall(threads)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM