diff options
author | George Kraft <george.kraft@calxeda.com> | 2013-10-01 15:52:31 -0500 |
---|---|---|
committer | George Kraft <george.kraft@calxeda.com> | 2013-10-01 15:52:31 -0500 |
commit | d4bddc138d5887119a7844463cb8300cede1504d (patch) | |
tree | c305b767fd0b6aefb6036856a97351fd4408ccaa /cxmanage_api/tftp.py | |
parent | 133751e03e045d27b54530a0118e40ea1e1362be (diff) | |
parent | 52057843f2b4002095422069ae0abbfa360de9ed (diff) | |
download | cxmanage-win32_support.tar.gz |
Merge branch 'master' into win32_supportwin32_support
Diffstat (limited to 'cxmanage_api/tftp.py')
-rw-r--r-- | cxmanage_api/tftp.py | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/cxmanage_api/tftp.py b/cxmanage_api/tftp.py index 0b33db8..59e9774 100644 --- a/cxmanage_api/tftp.py +++ b/cxmanage_api/tftp.py @@ -37,6 +37,7 @@ import socket import logging import traceback +from datetime import datetime, timedelta from tftpy import TftpClient, TftpServer, setLogLevel from threading import Thread from cxmanage_api import temp_dir @@ -61,6 +62,14 @@ class InternalTftp(Thread): :type verbose: boolean """ + _default = None + + @staticmethod + def default(): + """ Return the default InternalTftp server """ + if InternalTftp._default == None: + InternalTftp._default = InternalTftp() + return InternalTftp._default def __init__(self, ip_address=None, port=0, verbose=False): super(InternalTftp, self).__init__() @@ -74,10 +83,18 @@ class InternalTftp(Thread): self.port = port self.start() - # Get the port we actually hosted on (this covers the port=0 case) - while not self.server.sock: - pass - self.port = self.server.sock.getsockname()[1] + # Get the port we actually hosted on + if port == 0: + deadline = datetime.now() + timedelta(seconds=10) + while datetime.now() < deadline: + try: + self.port = self.server.sock.getsockname()[1] + break + except (AttributeError, socket.error): + pass + else: + # don't catch the error on our last attempt + self.port = self.server.sock.getsockname()[1] def run(self): """ Run the server. Listens indefinitely. """ |