问题
In the following code, the first test passes, and the second one does not, which I find puzzling.
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'
Here's what I get:
$ ./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
This happens both on Ubuntu (Python 2.6 and paramiko 1.7.6) and Debian (Python 2.7 and paramiko 1.7.7).
If I run test2
first, I only get the stack trace, meaning test2
indeed fails.
回答1:
Ok, I've verified it on debian/python2.6/paramiko1.7.6.
The reason is that the client
object goes out of scope in get_sftp
(and closes the "channel"). If you cange it so you return the client:
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()
then everything will work (the function name should probably be changed..).
来源:https://stackoverflow.com/questions/11925895/what-is-the-difference-between-calling-open-sftp-locally-and-via-a-separate-fu