python - 想要从用户那里获取 IP 地址以手动输入(而不是通过他们的位置)
问题描述
这是代码
在forms.py
from django import forms
class CmdForm(forms.Form):
ip_address = forms.CharField(label='Enter IP address:')
command = forms.CharField(label='Command to execute:')
在 Views.py 中
from django.shortcuts import render
from first_app.forms import CmdForm
from django.http import HttpResponse
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
devices = {
'device_type':'cisco_ios',
'ip':'ip_address',
'username':'mee',
'password':'12345',
'secret':'12345',
}
ipInsert = request.POST.get('ip_address', '')
cmd = request.POST.get('command', '')
netconnect = ConnectHandler(**devices)
#print("connection established with", devices['ip'])
getIP = netconnect.send_command(ipInsert)
output = netconnect.send_command(cmd)
return render(request,'first_app/forms.html', {'form': form, 'output':output, 'getIP':getIP})
else:
form = CmdForm()
return render(request,'first_app/forms.html', {'form': form})
else:
return render(request,'first_app/forms.html', {})
但是我得到了错误: -
NetMikoTimeoutException at /Automation_page/ 连接到设备超时:cisco_ios ip_address:22 请求方法:POST 请求 URL:http: //127.0.0.1 :8000/Automation_page/ Django 版本:2.2.3 异常类型:NetMikoTimeoutException 异常值:
连接到设备超时:cisco_ios ip_address:22 异常位置:C:\Users\karti\AppData\Local\Programs\Python\Python37-32\lib\site-packages\netmiko\base_connection.py 在建立连接,第 864 行 Python 可执行文件: C:\Users\karti\AppData\Local\Programs\Python\Python37-32\python.exe Python 版本:3.7.3 Python 路径:
['K:\Work\DevNet\first_project', 'C:\Users\karti\AppData\Local\Programs\Python\Python37-32\python37.zip', 'C:\Users\karti\AppData\Local\Programs \Python\Python37-32\DLLs', 'C:\Users\karti\AppData\Local\Programs\Python\Python37-32\lib', 'C:\Users\karti\AppData\Local\Programs\Python\Python37 -32', 'C:\Users\karti\AppData\Local\Programs\Python\Python37-32\lib\site-packages']
为什么我的设备已启动并正在运行,但我却超时了。
感谢那些愿意提供帮助的人。!:-)
解决方案
可能是这条线
'ip':'ip_address',
应该读
'ip': form.cleaned_data['ip_address'],
推荐阅读
- php - 从 Guzzle 流中获取项目
- python - f.write,如何修复页面顶部的标题?
- javascript - jQuery Modal 在第三次点击时关闭
- python - Tesseract OCR 准确性对图像中文本数量的依赖性
- php - 有没有办法从 laravel 中删除 algolia/scout?
- gatsby - Google 自定义搜索框不会在我的 Gatsby 项目中首次加载时出现,仅在重新加载或刷新时出现
- python - SyntaxError: 'break' outside loop,这是显示的错误
- html - 如果表格行到达页面末尾,则创建新的 DIN A4
- python - 如何在下面的条形图中删除额外的 X 标签?
- .net-core - .net 核心 API 中的混合身份验证