summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoradehad <26027314+adehad@users.noreply.github.com>2021-10-21 18:52:19 +0100
committeradehad <26027314+adehad@users.noreply.github.com>2021-10-21 18:52:19 +0100
commit11865abc65c7c096e8a415eda35b684140334ea7 (patch)
tree023b4b8dcba698f71fd3c3d5b83f113feb5fbb60
parent157dac9a07fb3fccbccec3162ac40d8f07559450 (diff)
parent77ba33dee44b3d889781db32cfcf99a4a2ada4dd (diff)
downloadtftpy-11865abc65c7c096e8a415eda35b684140334ea7.tar.gz
Merge remote-tracking branch 'upstream/master'
-rw-r--r--.gitignore2
-rwxr-xr-xbin/tftpy_server.py2
-rw-r--r--tftpy/TftpClient.py1
-rw-r--r--tftpy/TftpContexts.py26
-rw-r--r--tftpy/TftpPacketFactory.py1
-rw-r--r--tftpy/TftpPacketTypes.py3
-rw-r--r--tftpy/TftpServer.py28
-rw-r--r--tftpy/TftpStates.py17
-rw-r--r--tftpy/__init__.py6
9 files changed, 46 insertions, 40 deletions
diff --git a/.gitignore b/.gitignore
index c8d4918..1e88470 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,3 +18,5 @@ tags
.DS_Store
test.log
tftpy.egg-info
+# PyCharm
+.idea/
diff --git a/bin/tftpy_server.py b/bin/tftpy_server.py
index 64c86fa..8404ac3 100755
--- a/bin/tftpy_server.py
+++ b/bin/tftpy_server.py
@@ -67,7 +67,7 @@ def main():
)
handler.setFormatter(debug_formatter)
elif options.quiet:
- log.setLevel(logging.WARN)
+ log.setLevel(logging.WARNING)
if not options.root:
parser.print_help()
diff --git a/tftpy/TftpClient.py b/tftpy/TftpClient.py
index 03522d1..3aac753 100644
--- a/tftpy/TftpClient.py
+++ b/tftpy/TftpClient.py
@@ -15,6 +15,7 @@ from .TftpShared import *
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
diff --git a/tftpy/TftpContexts.py b/tftpy/TftpContexts.py
index 4314730..08927e5 100644
--- a/tftpy/TftpContexts.py
+++ b/tftpy/TftpContexts.py
@@ -121,7 +121,7 @@ class TftpContext(object):
def __del__(self):
"""Simple destructor to try to call housekeeping in the end method if
- not called explicitely. Leaking file descriptors is not a good
+ not called explicitly. Leaking file descriptors is not a good
thing."""
self.end()
@@ -137,7 +137,7 @@ class TftpContext(object):
def end(self, close_fileobj=True):
"""Perform session cleanup, since the end method should always be
- called explicitely by the calling code, this works better than the
+ called explicitly by the calling code, this works better than the
destructor.
Set close_fileobj to False so fileobj can be returned open."""
log.debug("in TftpContext.end - closing socket")
@@ -147,12 +147,16 @@ class TftpContext(object):
self.fileobj.close()
def gethost(self):
- "Simple getter method for use in a property."
+ """
+ Simple getter method for use in a property.
+ """
return self.__host
def sethost(self, host):
- """Setter method that also sets the address property as a result
- of the host that is set."""
+ """
+ Setter method that also sets the address property as a result
+ of the host that is set.
+ """
self.__host = host
self.address = socket.gethostbyname(host)
@@ -170,8 +174,10 @@ class TftpContext(object):
next_block = property(getNextBlock, setNextBlock)
def cycle(self):
- """Here we wait for a response from the server after sending it
- something, and dispatch appropriate action to that response."""
+ """
+ Here we wait for a response from the server after sending it
+ something, and dispatch appropriate action to that response.
+ """
try:
(buffer, (raddress, rport)) = self.sock.recvfrom(MAX_BLKSIZE)
except socket.timeout:
@@ -240,10 +246,12 @@ class TftpContextServer(TftpContext):
return "%s:%s %s" % (self.host, self.port, self.state)
def start(self, buffer):
- """Start the state cycle. Note that the server context receives an
+ """
+ Start the state cycle. Note that the server context receives an
initial packet in its start method. Also note that the server does not
loop on cycle(), as it expects the TftpServer object to manage
- that."""
+ that.
+ """
log.debug("In TftpContextServer.start")
self.metrics.start_time = time.time()
log.debug("Set metrics.start_time to %s", self.metrics.start_time)
diff --git a/tftpy/TftpPacketFactory.py b/tftpy/TftpPacketFactory.py
index 96baea4..452033a 100644
--- a/tftpy/TftpPacketFactory.py
+++ b/tftpy/TftpPacketFactory.py
@@ -13,6 +13,7 @@ from .TftpShared import *
log = logging.getLogger("tftpy.TftpPacketFactory")
+
class TftpPacketFactory(object):
"""This class generates TftpPacket objects. It is responsible for parsing
raw buffers off of the wire and returning objects representing them, via
diff --git a/tftpy/TftpPacketTypes.py b/tftpy/TftpPacketTypes.py
index 338ec86..f828534 100644
--- a/tftpy/TftpPacketTypes.py
+++ b/tftpy/TftpPacketTypes.py
@@ -13,6 +13,7 @@ from .TftpShared import *
log = logging.getLogger("tftpy.TftpPacketTypes")
+
class TftpSession(object):
"""This class is the base class for the tftp client and server. Any shared
code should be in this class."""
@@ -422,7 +423,7 @@ class TftpPacketERR(TftpPacket):
return self
def decode(self):
- "Decode self.buffer, populating instance variables and return self."
+ """Decode self.buffer, populating instance variables and return self."""
buflen = len(self.buffer)
tftpassert(buflen >= 4, "malformed ERR packet, too short")
log.debug("Decoding ERR packet, length %s bytes", buflen)
diff --git a/tftpy/TftpServer.py b/tftpy/TftpServer.py
index 4eea82a..ed1d491 100644
--- a/tftpy/TftpServer.py
+++ b/tftpy/TftpServer.py
@@ -22,6 +22,7 @@ from .TftpShared import *
log = logging.getLogger("tftpy.TftpServer")
+
class TftpServer(TftpSession):
"""This class implements a tftp server object. Run the listen() method to
listen for client requests.
@@ -124,8 +125,7 @@ class TftpServer(TftpSession):
break
# Build the inputlist array of sockets to select() on.
- inputlist = []
- inputlist.append(self.sock)
+ inputlist = [self.sock]
for key in self.sessions:
inputlist.append(self.sessions[key].sock)
@@ -164,19 +164,15 @@ class TftpServer(TftpSession):
# which should safely work through NAT.
key = "%s:%s" % (raddress, rport)
- if not key in self.sessions:
- log.debug(
- "Creating new server context for session key = %s" % key
- )
- self.sessions[key] = TftpContextServer(
- raddress,
- rport,
- timeout,
- self.root,
- self.dyn_file_func,
- self.upload_open,
- retries=retries,
- )
+ if key not in self.sessions:
+ log.debug("Creating new server context for session key = %s" % key)
+ self.sessions[key] = TftpContextServer(raddress,
+ rport,
+ timeout,
+ self.root,
+ self.dyn_file_func,
+ self.upload_open,
+ retries=retries)
try:
self.sessions[key].start(buffer)
except TftpException as err:
@@ -200,7 +196,7 @@ class TftpServer(TftpSession):
log.debug("Matched input to session key %s" % key)
try:
self.sessions[key].cycle()
- if self.sessions[key].state == None:
+ if self.sessions[key].state is None:
log.info("Successful transfer.")
deletion_list.append(key)
except TftpException as err:
diff --git a/tftpy/TftpStates.py b/tftpy/TftpStates.py
index ce9d0af..5817d22 100644
--- a/tftpy/TftpStates.py
+++ b/tftpy/TftpStates.py
@@ -138,7 +138,7 @@ class TftpState(object):
log.debug("In sendError, being asked to send error %d", errorcode)
errpkt = TftpPacketERR()
errpkt.errorcode = errorcode
- if self.context.tidport == None:
+ if self.context.tidport is None:
log.debug("Error packet received outside session. Discarding")
else:
self.context.sock.sendto(
@@ -158,10 +158,8 @@ class TftpState(object):
self.context.last_pkt = pkt
def resendLast(self):
- "Resend the last sent packet due to a timeout."
- log.warning(
- "Resending packet %s on sessions %s" % (self.context.last_pkt, self)
- )
+ """Resend the last sent packet due to a timeout."""
+ log.warning("Resending packet %s on sessions %s" % (self.context.last_pkt, self))
self.context.metrics.resent_bytes += len(self.context.last_pkt.buffer)
self.context.metrics.add_dup(self.context.last_pkt)
sendto_port = self.context.tidport
@@ -259,8 +257,7 @@ class TftpServerState(TftpState):
if self.context.host != raddress or self.context.port != rport:
self.sendError(TftpErrors.UnknownTID)
log.error(
- "Expected traffic from %s:%s but received it "
- "from %s:%s instead."
+ "Expected traffic from %s:%s but received it from %s:%s instead."
% (self.context.host, self.context.port, raddress, rport)
)
# FIXME: increment an error count?
@@ -307,7 +304,7 @@ class TftpStateServerRecvRRQ(TftpServerState):
received an RRQ packet."""
def handle(self, pkt, raddress, rport):
- "Handle an initial RRQ packet as a server."
+ """Handle an initial RRQ packet as a server."""
log.debug("In TftpStateServerRecvRRQ.handle")
sendoack = self.serverInitial(pkt, raddress, rport)
path = self.full_path
@@ -383,7 +380,7 @@ class TftpStateServerRecvWRQ(TftpServerState):
os.mkdir(current, 0o700)
def handle(self, pkt, raddress, rport):
- "Handle an initial WRQ packet as a server."
+ """Handle an initial WRQ packet as a server."""
log.debug("In TftpStateServerRecvWRQ.handle")
sendoack = self.serverInitial(pkt, raddress, rport)
path = self.full_path
@@ -452,7 +449,7 @@ class TftpStateExpectACK(TftpState):
download."""
def handle(self, pkt, raddress, rport):
- "Handle a packet, hopefully an ACK since we just sent a DAT."
+ """Handle a packet, hopefully an ACK since we just sent a DAT."""
if isinstance(pkt, TftpPacketACK):
log.debug("Received ACK for packet %d" % pkt.blocknumber)
# Is this an ack to the one we just sent?
diff --git a/tftpy/__init__.py b/tftpy/__init__.py
index 082a5e5..7b630bc 100644
--- a/tftpy/__init__.py
+++ b/tftpy/__init__.py
@@ -14,10 +14,10 @@ import sys
import pkg_resources
-# Make sure that this is at least Python 2.7
-required_version = (2, 7)
+# Make sure that this is at least Python 3
+required_version = (3, 0)
if sys.version_info < required_version:
- raise ImportError("Requires at least Python 2.7")
+ raise ImportError("Requires at least Python 3.0")
from . import TftpContexts, TftpPacketFactory, TftpPacketTypes, TftpStates
from . import __name__ as pkg_name