Python 本地调用open_sftp()和通过单独的函数调用open_sftp()有什么区别?
在下面的代码中,第一个测试通过了,第二个测试没有通过,这让我感到困惑Python 本地调用open_sftp()和通过单独的函数调用open_sftp()有什么区别?,python,paramiko,Python,Paramiko,在下面的代码中,第一个测试通过了,第二个测试没有通过,这让我感到困惑 import paramiko def test1(): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect('10.0.0.107', username='test', password='test') sftp = client.o
import paramiko
def test1():
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('10.0.0.107', username='test', password='test')
sftp = client.open_sftp()
sftp.stat('/tmp')
sftp.close()
def get_sftp():
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('10.0.0.107', username='test', password='test')
return client.open_sftp()
def test2():
sftp = get_sftp()
sftp.stat('/tmp')
sftp.close()
if __name__ == '__main__':
test1()
print 'test1 done'
test2()
print 'test2 done'
以下是我得到的:
$ ./script.py
test1 done
Traceback (most recent call last):
File "./play.py", line 25, in <module>
test2()
File "./play.py", line 20, in test2
sftp.stat('/tmp')
File "/usr/lib/pymodules/python2.6/paramiko/sftp_client.py", line 337, in stat
t, msg = self._request(CMD_STAT, path)
File "/usr/lib/pymodules/python2.6/paramiko/sftp_client.py", line 627, in _request
num = self._async_request(type(None), t, *arg)
File "/usr/lib/pymodules/python2.6/paramiko/sftp_client.py", line 649, in _async_request
self._send_packet(t, str(msg))
File "/usr/lib/pymodules/python2.6/paramiko/sftp.py", line 172, in _send_packet
self._write_all(out)
File "/usr/lib/pymodules/python2.6/paramiko/sftp.py", line 138, in _write_all
raise EOFError()
EOFError
$。/script.py
测试1完成
回溯(最近一次呼叫最后一次):
文件“/play.py”,第25行,在
test2()
test2中第20行的文件“/play.py”
sftp.stat('/tmp')
文件“/usr/lib/pymodules/python2.6/paramiko/sftp_client.py”,第337行,在stat中
t、 msg=self.\u请求(CMD\u STAT,path)
文件“/usr/lib/pymodules/python2.6/paramiko/sftp_client.py”,第627行,在请求中
num=self.\u异步\u请求(类型(无),t,*arg)
文件“/usr/lib/pymodules/python2.6/paramiko/sftp_client.py”,第649行,在异步请求中
自发送数据包(t、str(msg))
文件“/usr/lib/pymodules/python2.6/paramiko/sftp.py”,第172行,在发送数据包中
自述(全部写出)
文件“/usr/lib/pymodules/python2.6/paramiko/sftp.py”,第138行,全部写入
提高EOR()
伊奥费罗
这在Ubuntu(Python2.6和paramiko1.7.6)和Debian(Python2.7和paramiko1.7.7)上都会发生
如果我先运行
test2
,我只会得到堆栈跟踪,这意味着test2
确实失败了。好的,我已经在debian/python2.6/paramiko1.7.6上验证了它
原因是客户端
对象超出了获取sftp
中的范围(并关闭了“通道”)。如果您将其保存以便返回客户端:
import paramiko
def get_sftp():
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('localhost', username='root', password='B4nan-purr(en)')
return client
def test2():
client = get_sftp()
sftp = client.open_sftp()
sftp.stat('/tmp')
sftp.close()
if __name__ == "__main__":
test2()
然后一切都会正常工作(函数名可能会更改。)好的,我已经在debian/python2.6/paramiko1.7.6上验证过了 原因是
客户端
对象超出了获取sftp
中的范围(并关闭了“通道”)。如果您将其保存以便返回客户端:
import paramiko
def get_sftp():
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('localhost', username='root', password='B4nan-purr(en)')
return client
def test2():
client = get_sftp()
sftp = client.open_sftp()
sftp.stat('/tmp')
sftp.close()
if __name__ == "__main__":
test2()
然后一切都会正常工作(函数名可能会被更改。)这看起来像是一个实际的根密码,注意:)它曾经是,但在这篇文章之前很久就消失了:-)如果
SSHClient
超出范围时,通道关闭了,这是否意味着无法创建get\u sftp()
函数,只接受ip、用户和密码,并返回打开的SFTP连接,而不返回SSHClient
对象?@ewok您正在评论2012年的一篇文章。。。您可能应该问一个新问题(但连接是客户端是连接),它看起来像一个实际的根密码,注意:)它曾经是,但在这篇文章之前很久就消失了:-)如果SSHClient
超出范围时,通道关闭了,这是否意味着无法创建get\u sftp()
函数,只接受ip、用户和密码,并返回打开的SFTP连接,而不返回SSHClient
对象?@ewok您正在评论2012年的一篇文章。。。您可能应该问一个新问题(但连接是客户端是连接)