From 6c21519ca43b1dba34d05371b42542e211aa1825 Mon Sep 17 00:00:00 2001 From: Isaiah Taylor Date: Wed, 25 Jul 2018 13:35:46 -0700 Subject: [PATCH 1/7] adding client function and returning lifecycle information --- README.rst | 16 ++++++++++++++++ ptftplib/tftpclient.py | 21 +++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index ef4b4eb..125a0cf 100644 --- a/README.rst +++ b/README.rst @@ -23,6 +23,22 @@ basic TFTP RFC1350 compliance mode, disabling all TFTP extensions for increased compatibility would you encouter any problem with your target system. +Usage as a Library +------------------ + +pTFTPd TFTP client can also be imported and used within a Python script. + +```from ptftpd import tftpclient + +client = tftpclient.client('tftpsite.com') +results = client.get(['-f', 'thefile.txt']) + +print(results[0] + 'kB') +# prints 55234 kB + +print(results[1] + ' kB/s') +# prints 100 kB/s``` + Installation ------------ diff --git a/ptftplib/tftpclient.py b/ptftplib/tftpclient.py index 4c13a03..9399e62 100755 --- a/ptftplib/tftpclient.py +++ b/ptftplib/tftpclient.py @@ -565,13 +565,13 @@ def get(self, args): .format(filename)) return False + transfer_speed = self.__get_speed(self.PTFTP_STATE.filesize, transfer_time) + print('Transfer complete, {} bytes ({:.2f} kB/s)' - .format(self.PTFTP_STATE.filesize, - self.__get_speed(self.PTFTP_STATE.filesize, - transfer_time))) + .format(self.PTFTP_STATE.filesize, transfer_speed)) self.PTFTP_STATE.file.close() os.remove(self.PTFTP_STATE.file.name) - return True + return (self.PTFTP_STATE.filesize, transfer_speed) def put(self, args): """ @@ -627,11 +627,11 @@ def put(self, args): print('Error: {}'.format(errmsg)) return False + transfer_speed = self.__get_speed(self.PTFTP_STATE.filesize, transfer_time) + print('Transfer complete, {} bytes ({:.2f} kB/s)' - .format(self.PTFTP_STATE.filesize, - self.__get_speed(self.PTFTP_STATE.filesize, - transfer_time))) - return True + .format(self.PTFTP_STATE.filesize, transfer_speed)) + return (self.PTFTP_STATE.filesize, transfer_speed) def mode(self, args): if len(args) > 1: @@ -709,6 +709,11 @@ def usage(): print(' This will discard other TFTP option values.') print() +def client(host=_PTFTP_DEFAULT_HOST, port=_PTFTP_DEFAULT_PORT, + mode=_PTFTP_DEFAULT_MODE, exts={}, rfc1350=False): + client = TFTPClient((host, port), exts, mode, rfc1350) + client.connect() + return client def main(): # TODO: convert to optparse From ea1abb1892acbfc47052ed1ee37a218931da8de6 Mon Sep 17 00:00:00 2001 From: Isaiah Taylor Date: Wed, 25 Jul 2018 13:39:34 -0700 Subject: [PATCH 2/7] updating readme --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 125a0cf..9a677ce 100644 --- a/README.rst +++ b/README.rst @@ -30,7 +30,7 @@ pTFTPd TFTP client can also be imported and used within a Python script. ```from ptftpd import tftpclient -client = tftpclient.client('tftpsite.com') +client = tftpclient.client(host='tftpsite.com', exts={'windowsize': 4}) results = client.get(['-f', 'thefile.txt']) print(results[0] + 'kB') From b577c22fa5ccf9caf087e72a435a09babe498a2b Mon Sep 17 00:00:00 2001 From: Isaiah Taylor Date: Wed, 25 Jul 2018 13:44:06 -0700 Subject: [PATCH 3/7] more readme updates --- README.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.rst b/README.rst index 9a677ce..4fef1c8 100644 --- a/README.rst +++ b/README.rst @@ -31,7 +31,10 @@ pTFTPd TFTP client can also be imported and used within a Python script. ```from ptftpd import tftpclient client = tftpclient.client(host='tftpsite.com', exts={'windowsize': 4}) + results = client.get(['-f', 'thefile.txt']) +### or +results = client.put(['thefile.txt']) print(results[0] + 'kB') # prints 55234 kB From 2913dac3d6665427b2bbb939243b3d335d1cc3dc Mon Sep 17 00:00:00 2001 From: Isaiah Taylor Date: Wed, 25 Jul 2018 21:37:52 -0700 Subject: [PATCH 4/7] fixing readme for rst --- README.rst | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index 4fef1c8..bea72d4 100644 --- a/README.rst +++ b/README.rst @@ -28,19 +28,20 @@ Usage as a Library pTFTPd TFTP client can also be imported and used within a Python script. -```from ptftpd import tftpclient +.. code:: python + from ptftpd import tftpclient -client = tftpclient.client(host='tftpsite.com', exts={'windowsize': 4}) + client = tftpclient.client(host='tftpsite.com', exts={'windowsize': 4}) -results = client.get(['-f', 'thefile.txt']) -### or -results = client.put(['thefile.txt']) + results = client.get(['-f', 'thefile.txt']) + ### or + results = client.put(['thefile.txt']) -print(results[0] + 'kB') -# prints 55234 kB + print(results[0] + 'kB') + # prints 55234 kB -print(results[1] + ' kB/s') -# prints 100 kB/s``` + print(results[1] + ' kB/s') + # prints 100 kB/s Installation ------------ From 02fe07ea119cf9a0e297aba45d1dbbe92988c7ac Mon Sep 17 00:00:00 2001 From: Isaiah Taylor Date: Wed, 25 Jul 2018 21:39:47 -0700 Subject: [PATCH 5/7] code block whitespace --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index bea72d4..216d844 100644 --- a/README.rst +++ b/README.rst @@ -29,6 +29,7 @@ Usage as a Library pTFTPd TFTP client can also be imported and used within a Python script. .. code:: python + from ptftpd import tftpclient client = tftpclient.client(host='tftpsite.com', exts={'windowsize': 4}) From 38d717d75302a9444a4130c346a5d8008c332e8a Mon Sep 17 00:00:00 2001 From: Isaiah Taylor Date: Wed, 25 Jul 2018 21:40:29 -0700 Subject: [PATCH 6/7] readme spacing fix --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 216d844..facbf73 100644 --- a/README.rst +++ b/README.rst @@ -38,7 +38,7 @@ pTFTPd TFTP client can also be imported and used within a Python script. ### or results = client.put(['thefile.txt']) - print(results[0] + 'kB') + print(results[0] + ' kB') # prints 55234 kB print(results[1] + ' kB/s') From 7f8f5ef8f9bbe525789e1a58028d427fc4c9bed9 Mon Sep 17 00:00:00 2001 From: Isaiah Taylor Date: Wed, 25 Jul 2018 21:41:21 -0700 Subject: [PATCH 7/7] moved new usage info after installation information --- README.rst | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/README.rst b/README.rst index facbf73..512b2d6 100644 --- a/README.rst +++ b/README.rst @@ -23,26 +23,6 @@ basic TFTP RFC1350 compliance mode, disabling all TFTP extensions for increased compatibility would you encouter any problem with your target system. -Usage as a Library ------------------- - -pTFTPd TFTP client can also be imported and used within a Python script. - -.. code:: python - - from ptftpd import tftpclient - - client = tftpclient.client(host='tftpsite.com', exts={'windowsize': 4}) - - results = client.get(['-f', 'thefile.txt']) - ### or - results = client.put(['thefile.txt']) - - print(results[0] + ' kB') - # prints 55234 kB - - print(results[1] + ' kB/s') - # prints 100 kB/s Installation ------------ @@ -68,6 +48,29 @@ installation, you may need to specify the Python module search path with tftp> + +Usage as a Library +------------------ + +pTFTPd TFTP client can also be imported and used within a Python script. + +.. code:: python + + from ptftpd import tftpclient + + client = tftpclient.client(host='tftpsite.com', exts={'windowsize': 4}) + + results = client.get(['-f', 'thefile.txt']) + ### or + results = client.put(['thefile.txt']) + + print(results[0] + ' kB') + # prints 55234 kB + + print(results[1] + ' kB/s') + # prints 100 kB/s + + TFTP server and client ----------------------