From ed6b6f1f66c99f04ba15a3eaa59b23f7cbf4fe3d Mon Sep 17 00:00:00 2001 From: "Michael P. Soulier" Date: Tue, 15 May 2018 11:58:51 -0400 Subject: Rationalized module includes and logging. --- bin/tftpy_client.py | 31 ++++++++++++++++++++----------- bin/tftpy_server.py | 21 +++++++++++++++------ 2 files changed, 35 insertions(+), 17 deletions(-) (limited to 'bin') diff --git a/bin/tftpy_client.py b/bin/tftpy_client.py index 2e3b92d..ab7d0b5 100755 --- a/bin/tftpy_client.py +++ b/bin/tftpy_client.py @@ -4,7 +4,15 @@ import sys, logging, os from optparse import OptionParser import tftpy -logging.basicConfig() +log = logging.getLogger('tftpy') +log.setLevel(logging.INFO) + +# console handler +handler = logging.StreamHandler() +handler.setLevel(logging.DEBUG) +default_formatter = logging.Formatter('[%(asctime)s] %(message)s') +handler.setFormatter(default_formatter) +log.addHandler(handler) def main(): usage="" @@ -77,20 +85,21 @@ def main(): self.out = out def progresshook(self, pkt): - if isinstance(pkt, tftpy.TftpPacketDAT): + if isinstance(pkt, tftpy.TftpPacketTypes.TftpPacketDAT): self.progress += len(pkt.data) self.out("Transferred %d bytes" % self.progress) elif isinstance(pkt, tftpy.TftpPacketOACK): self.out("Received OACK, options are: %s" % pkt.options) if options.debug: - tftpy.setLogLevel(logging.DEBUG) + log.setLevel(logging.DEBUG) + # increase the verbosity of the formatter + debug_formatter = logging.Formatter('[%(asctime)s%(msecs)03d] %(levelname)s [%(name)s:%(lineno)s] %(message)s') + handler.setFormatter(debug_formatter) elif options.quiet: - tftpy.setLogLevel(logging.WARNING) - else: - tftpy.setLogLevel(logging.INFO) + log.setLevel(logging.WARNING) - progresshook = Progress(tftpy.log.info).progresshook + progresshook = Progress(log.info).progresshook tftp_options = {} if options.blksize: @@ -98,10 +107,10 @@ def main(): if options.tsize: tftp_options['tsize'] = 0 - tclient = tftpy.TftpClient(options.host, - int(options.port), - tftp_options, - options.localip) + tclient = tftpy.TftpClient.TftpClient(options.host, + int(options.port), + tftp_options, + options.localip) try: if options.download: if not options.output: diff --git a/bin/tftpy_server.py b/bin/tftpy_server.py index 89abb1e..d09d29f 100755 --- a/bin/tftpy_server.py +++ b/bin/tftpy_server.py @@ -4,7 +4,15 @@ import sys, logging from optparse import OptionParser import tftpy -logging.basicConfig() +log = logging.getLogger('tftpy') +log.setLevel(logging.INFO) + +# console handler +handler = logging.StreamHandler() +handler.setLevel(logging.DEBUG) +default_formatter = logging.Formatter('[%(asctime)s] %(message)s') +handler.setFormatter(default_formatter) +log.addHandler(handler) def main(): usage="" @@ -37,17 +45,18 @@ def main(): options, args = parser.parse_args() if options.debug: - tftpy.setLogLevel(logging.DEBUG) + log.setLevel(logging.DEBUG) + # increase the verbosity of the formatter + debug_formatter = logging.Formatter('[%(asctime)s%(msecs)03d] %(levelname)s [%(name)s:%(lineno)s] %(message)s') + handler.setFormatter(debug_formatter) elif options.quiet: - tftpy.setLogLevel(logging.WARN) - else: - tftpy.setLogLevel(logging.INFO) + log.setLevel(logging.WARN) if not options.root: parser.print_help() sys.exit(1) - server = tftpy.TftpServer(options.root) + server = tftpy.TftpServer.TftpServer(options.root) try: server.listen(options.ip, options.port) except tftpy.TftpException as err: -- cgit v1.2.1