在函数内部返回值

时间:2019-05-11 04:57:30

标签: python flask

我有python代码通过json帖子循环并连接到网络设备。一切正常,但我无法返回到json客户端邮递员。 Python 3. 4烧瓶。我尝试了许多不同的解决方案。我要做的就是从netmiko send命令返回结果

from flask import Flask, jsonify, request
import netmiko
from netmiko.ssh_autodetect import SSHDetect
from netmiko.ssh_exception import NetMikoTimeoutException
import time
import gevent

app = Flask(__name__)

@app.route('/myuri', methods=['GET','POST', 'DELETE'])

def post():
    # Authentication
    headers = request.headers
    auth = headers.get("header key")
    if auth == 'my key':

        def firewall(command):
            src_a = command[0]
            src_p = command[1]
            dst_a = command[2]
            dst_p = command[3]
            p_col = command[4]
            p_show = command[5]
            p_push = command[6]

            ip = "1.1.1.1"
            username = "bla"
            password = "bla"
            device = {"device_type": "autodetect", "host": ip,
                       "username": username, "password": password}

            while True:
                try:
                    guesser = SSHDetect(**device)
                    best_match = guesser.autodetect()
                    print(best_match)
                    if "None" in str(best_match):
                        continue
                    if "true" in str(p_show) and "juniper_junos" in 
                    str(best_match):
                        device["device_type"] = best_match
                        connection = netmiko.ConnectHandler(**device)
                        time.sleep(1)
                        connection.enable()
                        resp = connection.send_command('show 
           configuration | display json | match ' + str(src_a))
                        resp1 = connection.send_command('show 
           configuration | display json | match ' + str(src_p))
                        resp2 = connection.send_command('show 
           configuration | display json | match ' + str(dst_a))
                        resp3 = connection.send_command('show 
            configuration | display json | match ' + str(dst_p))
                        connection.disconnect()
                        time.sleep(1)
                        returns = resp, resp1, resp2, resp3
                        print(returns) # this prints fine !!!!!
                        return return # Can't  return back !!!!!!

                except NetMikoTimeoutException:
                    return "Timeout Error" ### Note can't return this!

        commands = []
        data = request.get_json(force=True)
        for x in data["firewall"]:
            if 'SourceAddress' in x:
                commands.append((x['SourceAddress'], x['SourcePort'], 
                x['DestinationAddress'], x['DestinationPort'],
                x['Protocol'], x['show'], x['push']))

        threads = [gevent.spawn(firewall, command) for command in 
        commands]
        gevent.joinall(threads)
        return "done" ###### how do i return the returns in function 
                            Firewall

    else:
        return jsonify({"message": "ERROR: Unauthorized"}), 401

python能够自动检测设备并登录获取信息,我可以打印所有信息,只是无法获得这些回报而返回enter code here

2 个答案:

答案 0 :(得分:2)

return 是一个关键字,代码中包含数据的变量是 returns

return returns # Will work !!!!!!

答案 1 :(得分:0)

您的退货单有误

  

返回return#无法返回!!!!!!