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
24 changes: 24 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ basic TFTP RFC1350 compliance mode, disabling all TFTP extensions for
increased compatibility would you encouter any problem with your target
system.


Installation
------------

Expand All @@ -47,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})
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/exts/opts/


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

Expand Down
21 changes: 13 additions & 8 deletions ptftplib/tftpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're in there, would you mind moving this and the line below (the close and the remove) in a finally block of the try/except above (line 567)?

os.remove(self.PTFTP_STATE.file.name)
return True
return (self.PTFTP_STATE.filesize, transfer_speed)

def put(self, args):
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/exts/opts/

client = TFTPClient((host, port), exts, mode, rfc1350)
client.connect()
return client

def main():
# TODO: convert to optparse
Expand Down