diff options
author | Guido van Rossum <guido@python.org> | 1992-11-05 23:01:42 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1992-11-05 23:01:42 (GMT) |
commit | c68a40183b2f66c7034b2a68c1b7042527ea36a2 (patch) | |
tree | fdf56cf491ea9122a72531ca313f985ce852e18d /Lib/ftplib.py | |
parent | c567c60135a33b23cd5f0363e4a80b2d21de81d9 (diff) | |
download | cpython-c68a40183b2f66c7034b2a68c1b7042527ea36a2.zip cpython-c68a40183b2f66c7034b2a68c1b7042527ea36a2.tar.gz cpython-c68a40183b2f66c7034b2a68c1b7042527ea36a2.tar.bz2 |
Use getsockname() if it exists
Diffstat (limited to 'Lib/ftplib.py')
-rw-r--r-- | Lib/ftplib.py | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 4e74090..ed588b5 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -48,12 +48,11 @@ CRLF = '\r\n' # Next port to be used by makeport(), with PORT_OFFSET added +# (This is now only used when the python interpreter doesn't support +# the getsockname() method yet) nextport = 0 PORT_OFFSET = 40000 PORT_CYCLE = 1000 -# XXX This is a nuisance: when using the program several times in a row, -# reusing the port doesn't work and you have to edit the first port -# assignment... We need getsockname()! # The class itself @@ -152,11 +151,10 @@ class FTP: self.putcmd(cmd) return self.getresp() - # Send a command and ignore the response, which must begin with '2' + # Send a command and expect a response beginning with '2' def voidcmd(self, cmd): - resp = self.sendcmd(cmd) - if resp[0] <> '2': - raise error_reply, resp + self.putcmd(cmd) + self.voidresp() # Send a PORT command with the current host and the given port number def sendport(self, port): @@ -171,11 +169,20 @@ class FTP: # Create a new socket and send a PORT command for it def makeport(self): global nextport - port = nextport + PORT_OFFSET - nextport = (nextport + 1) % PORT_CYCLE sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.bind('', port) - sock.listen(0) + try: + getsockname = sock.getsockname + except AttributeError: + if self.debugging > 1: + print '*** getsockname not supported', + print '-- using manual port assignment ***' + port = nextport + PORT_OFFSET + nextport = (nextport + 1) % PORT_CYCLE + sock.bind('', port) + getsockname = None + sock.listen(0) # Assigns the port if not explicitly bound + if getsockname: + host, port = getsockname() resp = self.sendport(port) return sock |