Skip to content

Commit bf2ebd0

Browse files
author
Pan
committed
Updated SFTP readdir to be used in non-blocking mode, added integration test.
Fixed use of SFTP opendir as context manager. Updated sftp non-blocking read and added non-blocking readdir example. Updated changelog.
1 parent 2abc1cc commit bf2ebd0

File tree

8 files changed

+767
-3497
lines changed

8 files changed

+767
-3497
lines changed

Changelog.rst

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@ Change Log
22
=============
33

44
0.5.0
5-
------
5+
++++++
66

77
Changes
8-
_________
8+
----------
99

10-
* Implemented SFTP statvfs and SFTP handle fstatvfs methods and integration tests.
10+
* Implemented SFTP statvfs and SFTP handle fstatvfs methods.
1111
* Implemented SFTPStatVFS extension class for file system statistics.
1212
* SFTP read and readdir functions now return size/error code along with data.
1313
* SFTP handle fstat now returns attributes.
14-
* Implemented SFTP handle readdir* methods as python generators,
15-
* SFTP openddir can now be used as a context manager.
14+
* Implemented SFTP handle readdir* methods as python generators.
1615
* Block directions function renamed to match libssh2.
1716
* Example scripts.
1817
* All session authentication methods now raise ``AuthenticationError`` on failure.
1918

19+
Fixes
20+
---------
21+
22+
* SFTP readdir functions can now be used in non-blocking mode
23+
* Use of SFTP openddir via context manager
2024

2125
0.4.0
22-
------
26+
+++++++++
2327

2428
Changes
25-
________
29+
---------
2630

2731
* Implemented SCP send and recv methods, all versions.
2832
* Conditional compilation of features requiring newer versions of libssh2.
@@ -34,21 +38,21 @@ ________
3438

3539

3640
0.3.1
37-
------
41+
++++++++++
3842

3943
Changes
40-
_________
44+
----------
4145

4246
* Added context manager to SFTP handle
4347
* Implemented SFTP write, seek, stat, fstat and last_error methods.
4448
* Implemented SFTPAttribute object creation and de-allocation - added unit test.
4549

4650

4751
0.3.0
48-
--------
52+
++++++++
4953

5054
Changes
51-
________
55+
----------
5256

5357
* Updated API
5458
* Updated session, channel, agent and pkey to accept any string type arguments.

examples/nonblocking_sftp_read.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/python
22

3-
"""Example script for SFTP read"""
3+
"""Example script for non-blocking SFTP read"""
44

55
from __future__ import print_function
66

@@ -62,8 +62,9 @@ def main():
6262
size, data = fh.read()
6363
for size, data in fh:
6464
pass
65-
# fh.close()
66-
# Handle is closed when object is garbage collected
65+
# Handle is also closed when object is garbage collected.
66+
# It is safe to close it explicitly as well.
67+
fh.close()
6768
print("Finished file read in %s" % (datetime.now() - now,))
6869

6970

examples/nonblocking_sftp_readdir.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/python
2+
3+
"""Example script for non-blocking SFTP readdir"""
4+
5+
from __future__ import print_function
6+
7+
import argparse
8+
import socket
9+
import os
10+
import pwd
11+
from datetime import datetime
12+
13+
from ssh2.session import Session
14+
from ssh2.utils import wait_socket
15+
from ssh2.error_codes import LIBSSH2_ERROR_EAGAIN
16+
17+
18+
USERNAME = pwd.getpwuid(os.geteuid()).pw_name
19+
20+
parser = argparse.ArgumentParser()
21+
22+
23+
parser.add_argument('dir', help="Directory to read")
24+
parser.add_argument('--host', dest='host',
25+
default='localhost',
26+
help='Host to connect to')
27+
parser.add_argument('--port', dest='port', default=22,
28+
help="Port to connect on", type=int)
29+
parser.add_argument('-u', dest='user', default=USERNAME,
30+
help="User name to authenticate as")
31+
32+
33+
def main():
34+
args = parser.parse_args()
35+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
36+
sock.connect((args.host, args.port))
37+
s = Session()
38+
s.handshake(sock)
39+
s.agent_auth(args.user)
40+
sftp = s.sftp_init()
41+
now = datetime.now()
42+
print("Starting read for remote dir %s" % (args.dir,))
43+
with sftp.opendir(args.dir) as fh:
44+
# Can set blocking to false at any point, as long as the
45+
# libssh2 operations support running in non-blocking mode.
46+
s.set_blocking(False)
47+
for size, buf, attrs in fh.readdir():
48+
if size == LIBSSH2_ERROR_EAGAIN:
49+
print("Would block on readdir, waiting on socket..")
50+
wait_socket(sock, s)
51+
continue
52+
print(buf)
53+
print("Finished read dir in %s" % (datetime.now() - now,))
54+
55+
56+
if __name__ == "__main__":
57+
main()

0 commit comments

Comments
 (0)