'HostKeys' object has no attribute 'has_key'
我是使用paramiko通过sftp上传文件的方法,但我得到了这个错误。
ri@ri-desktop:~/workspace/ssh$ python testssh.py
Traceback (most recent call last):
File"testssh.py", line 75, in
if host_keys.has_key(hostname):
AttributeError: 'HostKeys' object has no attribute 'has_key'
这是我的test.py中的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | hostkeytype = None hostkey = None files_copied = 0 try: host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))<br/> except IOError: try: # try ~/ssh/ too, e.g. on windows host_keys = paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts')) except IOError: print '*** Unable to open host keys file' host_keys = {} if host_keys.has_key(hostname): hostkeytype = host_keys[hostname].keys()[0] hostkey = host_keys[hostname][hostkeytype] print 'Using host key of type {0}'.format(hostkeytype) |
1 2 | paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts')) paramiko.util.load_host_keys(os.path.expanduser('~/ssh/known_hosts')) |
其中一个函数成功运行并返回一个没有
因为您使用的是python 2.x,所以问题应该是这些函数返回的某些东西没有
我看过paramiko.util的源代码,实际上,
因为hostkeys类的docstring说
A
.HostKeys object can be treated like a dict; any dict lookup is
equivalent to callinglookup .
你可以用这个代替
1 | if hostname in host_keys: |
您使用的是什么版本的python?因为
Removed. dict.has_key() – use the in operator instead.
来源:https://docs.python.org/3.1/whatsnew/3.0.html
只需通过添加以下代码来测试主机密钥是否为字典:
在