Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions examples/scp_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ def _prepare_sock(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.hostname, self.port))
self.sock.setblocking(1)
except Exception, e:
print "SockError: Can't connect socket to %s:%d" % (self.hostname, self.port)
print e
except Exception as e:
print("SockError: Can't connect socket to %s:%d" % (self.hostname, self.port))
print(e)

try:
self.session = libssh2.Session()
self.session.set_banner()
self.session.startup(self.sock)
self.session.userauth_password(self.username, self.password)
except Exception, e:
print "SSHError: Can't startup session"
print e
except Exception as e:
print("SSHError: Can't startup session")
print(e)

def send(self, remote_path, mode=0644):
datas=""
Expand All @@ -71,7 +71,7 @@ def __del__(self):

if __name__ == '__main__' :
if len(sys.argv) == 1:
print usage
print(usage)
sys.exit(1)
myscp = MySCPClient(
hostname=sys.argv[1],
Expand Down
18 changes: 9 additions & 9 deletions examples/sftp_listdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ def _prepare_sock(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.hostname, self.port))
self.sock.setblocking(1)
except Exception, e:
print "SockError: Can't connect socket to %s:%d" % (self.hostname, self.port)
print e
except Exception as e:
print("SockError: Can't connect socket to %s:%d" % (self.hostname, self.port))
print(e)

try:
self.session = libssh2.Session()
self.session.set_banner()
self.session.startup(self.sock)
self.session.userauth_password(self.username, self.password)
except Exception, e:
print "SSHError: Can't startup session"
print e
except Exception as e:
print("SSHError: Can't startup session")
print(e)

# use low level layer because we don't yet provide High layer for sftp
self.sftp = self.session._session.sftp_init()
Expand All @@ -60,10 +60,10 @@ def listdir(self, remote_path='/tmp'):
while True:
data = self.sftp.readdir(handle)
if not data: break
print data
print(data)

for file, attribute in self.sftp.listdir(handle):
print file, attribute
print(file, attribute)

self.sftp.close(handle)

Expand All @@ -73,7 +73,7 @@ def __del__(self):

if __name__ == '__main__' :
if len(sys.argv) == 1:
print usage
print(usage)
sys.exit(1)
mysftp = MySFTPClient(
hostname=sys.argv[1],
Expand Down
18 changes: 9 additions & 9 deletions examples/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def _prepare_sock(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.hostname, self.port))
self.sock.setblocking(1)
except Exception, e:
print "SockError: Can't connect socket to %s:%d" % (self.hostname, self.port)
print e
except Exception as e:
print("SockError: Can't connect socket to %s:%d" % (self.hostname, self.port))
print(e)

try:
self.session = libssh2.Session()
Expand All @@ -58,9 +58,9 @@ def _prepare_sock(self):
sys.stdout.write("Authentication that can continue %s\r\n" % auth_list)
self.session.userauth_password(self.username, self.password)

except Exception, e:
print "SSHError: Can't startup session"
print e
except Exception as e:
print("SSHError: Can't startup session")
print(e)

def run(self):

Expand Down Expand Up @@ -91,8 +91,8 @@ def run(self):
data = sys.stdin.read(1).replace('\n','\r\n')
channel.write(data)

except Exception,e:
print e
except Exception as e:
print(e)
finally:
channel.close()

Expand All @@ -103,7 +103,7 @@ def __del__(self):

if __name__ == '__main__' :
if len(sys.argv) == 1:
print usage
print(usage)
sys.exit(1)

# save terminal settings
Expand Down
16 changes: 8 additions & 8 deletions examples/ssh_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def __init__(self, hostname, username, password, port=22):
my_print(self.session.last_error())
self.session.userauth_password(self.username,self.password)
my_print(self.session.last_error())
except Exception, e:
print str(e)
raise Exception, self.session.last_error()
except Exception as e:
print(str(e))
raise Exception(self.session.last_error())

self.channel = self.session.open_session()
my_print(self.session.last_error())
Expand All @@ -62,7 +62,7 @@ def execute(self, command="uname -a"):
data = self.channel.read(buffer)
if data == '' or data is None: break
my_print(type(data))
print data.strip()
print(data.strip().decode('utf-8'))

self.channel.close()

Expand All @@ -73,11 +73,11 @@ def __del__(self):
if __name__ == '__main__':
try:
if len(sys.argv) == 1:
print usage
print(usage)
sys.exit(1)
src = SSHRemoteClient(sys.argv[1], sys.argv[2], sys.argv[3])
src.execute(sys.argv[4])
except Exception, e:
print str(e)
except KeyboardInterrupt, e:
except Exception as e:
print(str(e))
except KeyboardInterrupt as e:
sys.exit(1)
14 changes: 7 additions & 7 deletions examples/ssh_exec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def _wait_select(self):
# return if not blocked
return

readfds = [self.sock] if (blockdir & 01) else []
writefds = [self.sock] if (blockdir & 02) else []
readfds = [self.sock] if (blockdir & 0x1) else []
writefds = [self.sock] if (blockdir & 0x2) else []
select(readfds, writefds, [])
return

Expand Down Expand Up @@ -108,7 +108,7 @@ def execute(self, cmd):
data1 = self.chan.read_ex()
while data1[0] > 0:
my_print('Received data')
print data1[1]
print(data1[1].encode('utf-8'))
data1 = self.chan.read_ex()


Expand All @@ -126,14 +126,14 @@ def __del__(self):
if __name__ == '__main__':
try:
if len(sys.argv) == 1:
print usage
print(usage)
sys.exit(1)
src = SSHRemoteClientNonBlocking(sys.argv[1], sys.argv[2], sys.argv[3])
src.startup()
src.auth()
src.open_channel()
src.execute(sys.argv[4])
except Exception, e:
print str(e)
except KeyboardInterrupt, e:
except Exception as e:
print(str(e))
except KeyboardInterrupt as e:
sys.exit(1)
18 changes: 9 additions & 9 deletions examples/ssh_publickey.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def _prepare_sock(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.hostname, self.port))
self.sock.setblocking(1)
except Exception, e:
print "SockError: Can't connect socket to %s:%d" % (self.hostname, self.port)
print e
except Exception as e:
print("SockError: Can't connect socket to %s:%d" % (self.hostname, self.port))
print(e)

try:
self.session = libssh2.Session()
Expand All @@ -62,9 +62,9 @@ def _prepare_sock(self):
# authentication
self.session.userauth_publickey_fromfile(self.username, self.public_key, self.private_key, self.password)

except Exception, e:
print "SSHError: Can't startup session"
raise e
except Exception as e:
print("SSHError: Can't startup session")
raise(e)

def run(self):

Expand Down Expand Up @@ -99,7 +99,7 @@ def run(self):
# Print a newline (in case user was sitting at prompt)
print('')
except Exception as e:
print e
print(e)
finally:
channel.close()

Expand All @@ -117,7 +117,7 @@ def argv_or(position, default):
return default

if len(sys.argv) == 1:
print usage
print(usage)
sys.exit(1)

# save terminal settings
Expand All @@ -132,7 +132,7 @@ def argv_or(position, default):
myssh.run()

finally:
print ''
print('')
# restore terminal settings
atexit.register(
termios.tcsetattr,
Expand Down
22 changes: 11 additions & 11 deletions examples/ssh_x11.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def trace(session):

if __name__ == '__main__':
if len(sys.argv) == 1:
print usage
print(usage)
sys.exit(1)

DEBUG=False
Expand All @@ -86,10 +86,10 @@ def trace(session):
try:
sock.connect((hostname, port))
sock.setblocking(0)
except Exception, e:
print "Can't connect socket to (%s:%d): %s" % (
except Exception as e:
print("Can't connect socket to (%s:%d): %s" % (
hostname, port, e
)
))
sys.exit(1)

# start session
Expand All @@ -99,8 +99,8 @@ def trace(session):
# trace session on stderr if DEBUG=True
trace(session)
session.startup(sock)
except SessionException, e:
print "Can't startup session: %s" % e
except SessionException as e:
print("Can't startup session: %s" % e)
sys.exit(1)

# register X11 callback
Expand All @@ -111,10 +111,10 @@ def trace(session):

try:
session.userauth_password(username, password)
except SessionException, e:
print "Failed to authenticate user with %s %s" % (
except SessionException as e:
print("Failed to authenticate user with %s %s" % (
username, password
)
))
sys.exit(1)

try:
Expand Down Expand Up @@ -179,8 +179,8 @@ def trace(session):
if channel.eof():
break

except ChannelException, e:
print "Channel exception: %s" % e
except ChannelException as e:
print("Channel exception: %s" % e)
finally:
channel.close()

Expand Down
8 changes: 4 additions & 4 deletions libssh2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#
__doc__ = """Python binding for libssh2 library"""

from version import *
from libssh2.version import *

from channel import ChannelException, Channel
from session import SessionException, Session
from sftp import SftpException, Sftp
from libssh2.channel import ChannelException, Channel
from libssh2.session import SessionException, Session
from libssh2.sftp import SftpException, Sftp

__all__ = [
'Channel',
Expand Down
22 changes: 21 additions & 1 deletion libssh2/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import _libssh2

from channel import Channel
from libssh2.channel import Channel

class SessionException(Exception):
"""
Expand Down Expand Up @@ -141,6 +141,26 @@ def set_trace(self, bitmask):
"""
self._session.set_trace(bitmask)

def keepalive_config(self, wantReply, interval):
"""
Set how often to send keepalives.

@param wantReply: Whether to request the server sends responses to keepalives
@type wantReply: bool
@param interval: How often to send keepalives (0 to disable)
@type interval: int
@param bitmask: bitmask on libssh2.LIBSSH2_TRACE_* constant
"""
self._session.keepalive_config(wantReply, interval)

def keepalive_send(self):
"""
Sends keepalive, if due. Call this to send keepalives from non-blocking code.

@return: seconds until next keepalive
@rtype: int
"""
return self._session.keepalive_send()

def scp_recv(self, remote_path):
"""
Expand Down
Empty file modified setup.py
100755 → 100644
Empty file.
Loading