Web2py 与 paramiko 用于 ssh 功能

Web2py with paramiko for ssh functionality

我有一个通过 web2py 构建的表单,我需要用它来验证和注册机器并将其存储到数据库中。验证未通过。我总是收到 "Houston there seems to be a problem with Machine name or super password" 的错误。如果我 运行 web2py 环境之外的 paramiko 脚本它工作正常。请帮忙。

表格详细信息:

db.define_table('nsksystem',
  Field('nskmachinename', length=128, requires = IS_NOT_EMPTY(error_message='Machine Name cant be empty'), label = T('Machine Name')),
  Field('nskpassword', 'password', length=512,readable=False, label=T('Machine Password')),
  Field('confirmnskpassword', 'password', length=512,readable=False, label=T('Confirm Machine Password'))        )

控制器:default.py(用于在插入前验证表单插入)

def machinevalidate(form):
  host = form.vars.nskmachinename
  user ='superman'
  pwd = form.vars.nskpassword

  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())    
  try:
    ssh.connect(host, username=user,password=pwd)
    return True
  except Exception, e:
    form.errors.nskpassword = 'Houston there seems to be a problem with Machine name or super password'
finally:
    ssh.close()

控制器:default.py(用于插入数据库)

@auth.requires_login()
def machine():
form = SQLFORM(db.nsksystem)
if form.process(onvalidation=machinevalidate).accepted:
    response.flash = 'The machine is now registered to the user.'
elif form.errors:
    response.flash = 'Form has errors'
else:
    response.flash = 'Please register a machine to your ID'
return dict(form=form)

根据建议我删除了 try/catch,这是错误的回溯。我在 Lib/ctypes 下有 wintypes.py。但是仍然无法弄清楚为什么会出现导入错误。

  Traceback (most recent call last):
  File "gluon/restricted.py", line 224, in restricted
  File "C:/web2py/applications/click/controllers/default.py", line 53, in <module>
  File "gluon/globals.py", line 393, in <lambda>
  File "gluon/tools.py", line 3444, in f
  File "C:/web2py/applications/click/controllers/default.py", line 39, in machine
    if form.process(onvalidation=machinevalidate).accepted:
   File "gluon/html.py", line 2303, in process
  File "gluon/html.py", line 2240, in validate
  File "gluon/sqlhtml.py", line 1461, in accepts
  File "gluon/html.py", line 2141, in accepts
  File "gluon/html.py", line 146, in call_as_list
  File "C:/web2py/applications/click/controllers/default.py", line 33, in machinevalidate
   ssh.connect(host, username=user,password=pwd)
   File "C:\Python27\lib\site-packages\paramiko\client.py", line 307, in connect
   look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)
 File "C:\Python27\lib\site-packages\paramiko\client.py", line 456, in _auth
self._agent = Agent()
  File "C:\Python27\lib\site-packages\paramiko\agent.py", line 332, in __init__
from . import win_pageant
 File "gluon/custom_import.py", line 105, in custom_importer
  File "C:\Python27\lib\site-packages\paramiko\win_pageant.py", line 25, in <module>
  import ctypes.wintypes
  File "gluon/custom_import.py", line 105, in custom_importer
 ImportError: No module named wintypes

找到解决方案... 我使用的是 web2py 的编译版本。当我使用源代码时,导入模块不再是头疼的事。感谢 Andrew Magee 的提醒...