diff options
author | adehad <26027314+adehad@users.noreply.github.com> | 2021-09-23 23:06:44 +0100 |
---|---|---|
committer | adehad <26027314+adehad@users.noreply.github.com> | 2021-10-11 23:08:42 +0100 |
commit | 9ea9b540251cc366442fa49ede72c5b17a1ad175 (patch) | |
tree | 218b3759cb58939bff29ff9a69996dafc6c7df38 /tftpy/TftpClient.py | |
parent | 10e456319c46268f76ec8b8aed5934995252a916 (diff) | |
download | tftpy-9ea9b540251cc366442fa49ede72c5b17a1ad175.tar.gz |
first attempt with github actions, various linting
Diffstat (limited to 'tftpy/TftpClient.py')
-rw-r--r-- | tftpy/TftpClient.py | 96 |
1 files changed, 63 insertions, 33 deletions
diff --git a/tftpy/TftpClient.py b/tftpy/TftpClient.py index 8b215a3..03522d1 100644 --- a/tftpy/TftpClient.py +++ b/tftpy/TftpClient.py @@ -5,20 +5,22 @@ instance of the client, and then use its upload or download method. Logging is performed via a standard logging object set in TftpShared.""" -import types import logging -from .TftpShared import * -from .TftpPacketTypes import * +import types + from .TftpContexts import TftpContextClientDownload, TftpContextClientUpload +from .TftpPacketTypes import * +from .TftpShared import * + +log = logging.getLogger("tftpy.TftpClient") -log = logging.getLogger('tftpy.TftpClient') class TftpClient(TftpSession): """This class is an implementation of a tftp client. Once instantiated, a download can be initiated via the download() method, or an upload via the upload() method.""" - def __init__(self, host, port=69, options={}, localip = ""): + def __init__(self, host, port=69, options={}, localip=""): TftpSession.__init__(self) self.context = None self.host = host @@ -26,13 +28,20 @@ class TftpClient(TftpSession): self.filename = None self.options = options self.localip = localip - if 'blksize' in self.options: - size = self.options['blksize'] + if "blksize" in self.options: + size = self.options["blksize"] tftpassert(int == type(size), "blksize must be an int") if size < MIN_BLKSIZE or size > MAX_BLKSIZE: raise TftpException("Invalid blksize: %d" % size) - def download(self, filename, output, packethook=None, timeout=SOCK_TIMEOUT, retries=DEF_TIMEOUT_RETRIES): + def download( + self, + filename, + output, + packethook=None, + timeout=SOCK_TIMEOUT, + retries=DEF_TIMEOUT_RETRIES, + ): """This method initiates a tftp download from the configured remote host, requesting the filename passed. It writes the file to output, which can be a file-like object or a path to a local file. If a @@ -48,34 +57,51 @@ class TftpClient(TftpSession): Note: If output is a hyphen, stdout is used.""" # We're downloading. log.debug("Creating download context with the following params:") - log.debug("host = %s, port = %s, filename = %s" % (self.host, self.iport, filename)) - log.debug("options = %s, packethook = %s, timeout = %s" % (self.options, packethook, timeout)) - self.context = TftpContextClientDownload(self.host, - self.iport, - filename, - output, - self.options, - packethook, - timeout, - retries=retries, - localip=self.localip) + log.debug( + "host = %s, port = %s, filename = %s" % (self.host, self.iport, filename) + ) + log.debug( + "options = %s, packethook = %s, timeout = %s" + % (self.options, packethook, timeout) + ) + self.context = TftpContextClientDownload( + self.host, + self.iport, + filename, + output, + self.options, + packethook, + timeout, + retries=retries, + localip=self.localip, + ) self.context.start() # Download happens here self.context.end() metrics = self.context.metrics - log.info('') + log.info("") log.info("Download complete.") if metrics.duration == 0: log.info("Duration too short, rate undetermined") else: - log.info("Downloaded %.2f bytes in %.2f seconds" % (metrics.bytes, metrics.duration)) + log.info( + "Downloaded %.2f bytes in %.2f seconds" + % (metrics.bytes, metrics.duration) + ) log.info("Average rate: %.2f kbps" % metrics.kbps) log.info("%.2f bytes in resent data" % metrics.resent_bytes) log.info("Received %d duplicate packets" % metrics.dupcount) - def upload(self, filename, input, packethook=None, timeout=SOCK_TIMEOUT, retries=DEF_TIMEOUT_RETRIES): + def upload( + self, + filename, + input, + packethook=None, + timeout=SOCK_TIMEOUT, + retries=DEF_TIMEOUT_RETRIES, + ): """This method initiates a tftp upload to the configured remote host, uploading the filename passed. It reads the file from input, which can be a file-like object or a path to a local file. If a packethook @@ -89,27 +115,31 @@ class TftpClient(TftpSession): after encountering a timeout. Note: If input is a hyphen, stdin is used.""" - self.context = TftpContextClientUpload(self.host, - self.iport, - filename, - input, - self.options, - packethook, - timeout, - retries=retries, - localip=self.localip) + self.context = TftpContextClientUpload( + self.host, + self.iport, + filename, + input, + self.options, + packethook, + timeout, + retries=retries, + localip=self.localip, + ) self.context.start() # Upload happens here self.context.end() metrics = self.context.metrics - log.info('') + log.info("") log.info("Upload complete.") if metrics.duration == 0: log.info("Duration too short, rate undetermined") else: - log.info("Uploaded %d bytes in %.2f seconds" % (metrics.bytes, metrics.duration)) + log.info( + "Uploaded %d bytes in %.2f seconds" % (metrics.bytes, metrics.duration) + ) log.info("Average rate: %.2f kbps" % metrics.kbps) log.info("%.2f bytes in resent data" % metrics.resent_bytes) log.info("Resent %d packets" % metrics.dupcount) |