mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Use getsockname() if it exists
This commit is contained in:
parent
c567c60135
commit
c68a40183b
1 changed files with 18 additions and 11 deletions
|
@ -48,12 +48,11 @@ CRLF = '\r\n'
|
||||||
|
|
||||||
|
|
||||||
# Next port to be used by makeport(), with PORT_OFFSET added
|
# 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
|
nextport = 0
|
||||||
PORT_OFFSET = 40000
|
PORT_OFFSET = 40000
|
||||||
PORT_CYCLE = 1000
|
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
|
# The class itself
|
||||||
|
@ -152,11 +151,10 @@ class FTP:
|
||||||
self.putcmd(cmd)
|
self.putcmd(cmd)
|
||||||
return self.getresp()
|
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):
|
def voidcmd(self, cmd):
|
||||||
resp = self.sendcmd(cmd)
|
self.putcmd(cmd)
|
||||||
if resp[0] <> '2':
|
self.voidresp()
|
||||||
raise error_reply, resp
|
|
||||||
|
|
||||||
# Send a PORT command with the current host and the given port number
|
# Send a PORT command with the current host and the given port number
|
||||||
def sendport(self, port):
|
def sendport(self, port):
|
||||||
|
@ -171,11 +169,20 @@ class FTP:
|
||||||
# Create a new socket and send a PORT command for it
|
# Create a new socket and send a PORT command for it
|
||||||
def makeport(self):
|
def makeport(self):
|
||||||
global nextport
|
global nextport
|
||||||
port = nextport + PORT_OFFSET
|
|
||||||
nextport = (nextport + 1) % PORT_CYCLE
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
sock.bind('', port)
|
try:
|
||||||
sock.listen(0)
|
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)
|
resp = self.sendport(port)
|
||||||
return sock
|
return sock
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue