返回函数中的值

2024-06-11 23:34:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有python代码通过json post循环并连接到网络设备。所有这些工作正常,但我不能回到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


Tags: inimportsrcsendjsonreturnifdevice
2条回答

你的报税单打错了

return return # Can't return back !!!!!!

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

return returns # Will work !!!!!!

相关问题 更多 >