簡體   English   中英

在函數內部返回值

[英]returning values inside a function

我有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

蟒蛇的作品找到設備自動檢測並登錄獲取信息,我可以打印所有信息,只是無法獲得這些返回信息返回enter code here

返回是一個關鍵字,在你的代碼與數據的變量是收益

return returns # Will work !!!!!!

您的退貨單有錯別字

return return#無法返回!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM