微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过单击混合了 Ajax 函数的 HTML 按钮启动 Python 脚本

如何解决通过单击混合了 Ajax 函数的 HTML 按钮启动 Python 脚本

一方面,我编写了一个 Python 脚本,它应该可以让我向网络设备发送一些命令。另一方面,我有一个 html 页面,它调用一个应该启动 Python 脚本的 Ajax 函数。 请在下面找到代码

  • HTML 页面和 ajax 函数

    <!--<h1 style="font-size:10px"> Launch Python script to Cisco Devices </h1>-->
      <input name="command" type="text" id="command">
      <input type="button" style="background-color:#3F7FBF;color:white;border-radius: 25%;" id='script' name="scriptbutton" value="Run Script Cisco" onclick="cisco_script()"></br>
      <script src="http://code.jquery.com/jquery-3.3.1.js"></script>
      <script>
              function cisco_script(){
                  $.ajax({
                    type:"POST",url: "cisco_script.py",data: {param: document.getElementById('command').value + "\n"},context: document.body
                  }).done(function() {
                   alert('finished python script');;
                  });
              }
          </script>
    
  • 还有python脚本:

    !/usr/bin/env python
    
    import sys
    import time
    import paramiko
    import os
    import cmd
    import datetime
    import cgi,cgitb
    
    cgitb.enable() # display error messages in web browser
    
    Create instance of FieldStorage
    data = cgi.FieldStorage()
    
    Get data from fields
    output = data["param"]
    
    set date and time
    Now = datetime.datetime.Now()
    
    authentication
    USER = 'login'
    PASSWORD = 'password'
    
    start FOR ...in
    f = open('cisco.device.list2')
    for ip in f.readlines():
            ip = ip.strip()
            ###session start
            client = paramiko.SSHClient()
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            client.connect(ip,username=USER,password=PASSWORD)
            ###ssh shell
            chan = client.invoke_shell()
            time.sleep(2)
            ###send new config
            chan.send('conf t\n')
            chan.send('int gi0/6\n')
            time.sleep(2)
            chan.send(output.value)
            time.sleep(5)
            ###close ssh session
            client.close()
            print (ip)
            f.close()

python 脚本本身运行良好。 一旦我尝试从文本输入字段启动它并单击按钮,它就不起作用。我在 9 毫秒 时收到内部服务器错误

[HTTP/1.1 500 Internal Server Error 9ms]

你能告诉我我的代码中的错误是什么吗?

提前致谢,祝一切顺利;-) !!!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。