Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8413231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:52:40+00:00 2026-06-10T00:52:40+00:00

In the following code, the first test passes, and the second one does not,

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:52:41+00:00Added an answer on June 10, 2026 at 12:52 am

    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..).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to DRY the following code and call the first initializer from the second?
Does the following code contain a memory leak of the first three characters in
The following code (without showing it all) is my first attempt at using classes.
I'm using the Code First approach and have the following Model: public class Person
In the following code, the first log statement shows a decimal as expected, but
I have the following code: Public Shared Function GetAvailableManufacturers() As List(Of Manufacturer) 'first get
I've in the following code the progressbar working on the first page load. after
The following is using EF 5.0.0-rc and Code First. In my design, I have
I've written the following code to create a three-column layout where the first and
In the following code snippet, if I go to the first open parenthesis (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.