0

在下面的代码中,我在使用 ATOM 文本编辑器的错误处理语句中遇到了 syatax 错误。django 用于 Web 界面,netmiko libaray 用于我的后端代码。

from django.shortcuts import render
from first_app.forms import CmdForm
from django.http import HttpResponse
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
from paramiko.ssh_exception import SSHException
from netmiko.ssh_exception import AuthenticationException
import datetime, time, sys
   # Create your views here.

def index(request):
    my_dict = {'insert_me': ""}
    return render(request,'first_app/index.html',context=my_dict)

def form_name_view(request):
    if request.method == "POST":
        form = CmdForm(request.POST)
        if form.is_valid():
            from netmiko import ConnectHandler
            ipInsert = request.POST.get('ip_address', '')
            devices = {
            'device_type':'cisco_ios',
            'ip':ipInsert,
            'username':'mee',
            'password':'12345',
            'secret':'12345',
            }
            cmd = request.POST.get('command', '')
            try:
                netconnect = ConnectHandler(**devices)
            except(AuthenticationException):
                print ('Authentication failed' + ipInsert)
            continue           #position 1
                continue       #posiiton 2
            getIP = netconnect.send_command(ipInsert)
            output = netconnect.send_command(cmd)
            now = time.strftime("%Y_%m_%d__%H_%M%S")
            file = sys.stdout
            file = open("C:/Users/karti/OneDrive/Desktop/frontend/ "+now +".txt", mode='w+')
            file.write("IP address is\n"+ ipInsert)
            file.write("\n\nCommand Executed: \n"+ cmd)
            file.write("\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
            file.write("\n\nOutput of Executed Command: \n\n\n"+output)
            file.close

            return render(request,'first_app/forms.html', {'form': form, 'output':output, 'getIP':getIP, 'date_time':now})
        else:
            form = CmdForm()
        return render(request,'first_app/forms.html', {'form': form})
    else:
        return render(request,'first_app/forms.html', {})

在位置 1 继续时出错

从 first_app 导入视图文件“K:\Work\DevNet\first_project\first_app\views.py”,第 33 行 continue ^ SyntaxError: 'continue' not proper in loop

在位置 2 继续时出错

文件“K:\Work\DevNet\first_project\first_app\views.py”,第 33 行 continue ^ SyntaxError: 'continue' not proper in loop

4

2 回答 2

1


下面的代码是真的:

cmd = request.POST.get('command', '')
try:
    netconnect = ConnectHandler(**devices)
except Exception as e:
    print ('Authentication failed' + ipInsert)
    continue
getIP = netconnect.send_command(ipInsert)
于 2019-07-15T06:19:29.060 回答
0
  • 在 python 中,由于对齐不正确,您的程序中可能存在错误,try并且Exception似乎没有对齐。
  • 每次尝试都应该有一个块,您可以在其中捕获从块中except抛出的异常。try
  • 此外,第 31 行似乎有错误,但发布的代码较少
于 2019-07-15T06:11:43.653 回答