异常处理中的python/Django语法错误

2024-06-28 20:24:55 发布

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

在下面的代码中,我在错误处理语句中得到syatax error' 我正在使用ATOM文本编辑器。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继续时出错

from first_app import views File "K:\Work\DevNet\first_project\first_app\views.py", line 33 continue ^ SyntaxError: 'continue' not properly in loop

在位置2继续时出错

File "K:\Work\DevNet\first_project\first_app\views.py", line 33 continue ^ SyntaxError: 'continue' not properly in loop


Tags: fromimportformappreturnrequesthtmlforms
2条回答
  • 在python中,由于对齐不正确,可能会出现错误,在程序中,tryException似乎没有对齐。你知道吗
  • 每次尝试都应该有一个except块,在那里您可以捕获从try块抛出的异常。你知道吗
  • 另外,第31行似乎有错误,但发布的代码较少


以下代码为真:

cmd = request.POST.get('command', '')
try:
    netconnect = ConnectHandler(**devices)
except Exception as e:
    print ('Authentication failed' + ipInsert)
    continue
getIP = netconnect.send_command(ipInsert)

相关问题 更多 >