异常处理中的python/Django语法错误 [英] python/Django syntax error in Exception handling
问题描述
在下面的代码中,我在错误处理语句"中收到syatax错误'我正在使用ATOM文本编辑器.django用于Web界面,netmiko libaray用于我的后端代码.
In the code below i m getting syatax error in Error handling statements' i m using ATOM text editor. django for web interface and netmiko libaray for my backend code.
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行继续^SyntaxError:继续"未正确循环
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继续时出错
文件"K:\ Work \ DevNet \ first_project \ first_app \ views.py",第33行继续^SyntaxError:继续"未正确循环
File "K:\Work\DevNet\first_project\first_app\views.py", line 33 continue ^ SyntaxError: 'continue' not properly in loop
推荐答案
以下代码是正确的:
codes below are true:
cmd = request.POST.get('command', '')
try:
netconnect = ConnectHandler(**devices)
except Exception as e:
print ('Authentication failed' + ipInsert)
continue
getIP = netconnect.send_command(ipInsert)
这篇关于异常处理中的python/Django语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!