diff options
author | Gregory P. Smith <greg@mad-scientist.com> | 2010-01-03 03:28:29 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@mad-scientist.com> | 2010-01-03 03:28:29 (GMT) |
commit | b4066374db45af2927eb5cde68f9b030eaec1b96 (patch) | |
tree | 1c09a2cc471c099ab2e1cbd8d4d29d30a38b3276 /Lib/socket.py | |
parent | 91ae4a1404fabc236a835cd5dd058f7e6b32062b (diff) | |
download | cpython-b4066374db45af2927eb5cde68f9b030eaec1b96.zip cpython-b4066374db45af2927eb5cde68f9b030eaec1b96.tar.gz cpython-b4066374db45af2927eb5cde68f9b030eaec1b96.tar.bz2 |
Merged revisions 77263-77264 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r77263 | gregory.p.smith | 2010-01-02 17:29:44 -0800 (Sat, 02 Jan 2010) | 4 lines
Adds an optional source_address parameter to socket.create_connection().
For use by issue3972.
........
r77264 | gregory.p.smith | 2010-01-02 18:06:07 -0800 (Sat, 02 Jan 2010) | 5 lines
issue3972: HTTPConnection and HTTPSConnection now support a
source_address parameter.
Also cleans up an annotation in the socket documentation.
........
Diffstat (limited to 'Lib/socket.py')
-rw-r--r-- | Lib/socket.py | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/Lib/socket.py b/Lib/socket.py index be019db..9133411 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -23,7 +23,8 @@ inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89) socket.getdefaulttimeout() -- get the default timeout value socket.setdefaulttimeout() -- set the default timeout value -create_connection() -- connects to an address, with an optional timeout +create_connection() -- connects to an address, with an optional timeout and + optional source address. [*] not available on all platforms! @@ -276,7 +277,8 @@ def getfqdn(name=''): _GLOBAL_DEFAULT_TIMEOUT = object() -def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT): +def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT, + source_address=None): """Connect to *address* and return the socket object. Convenience function. Connect to *address* (a 2-tuple ``(host, @@ -284,7 +286,9 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT): *timeout* parameter will set the timeout on the socket instance before attempting to connect. If no *timeout* is supplied, the global default timeout setting returned by :func:`getdefaulttimeout` - is used. + is used. If *source_address* is set it must be a tuple of (host, port) + for the socket to bind as a source address before making the connection. + An host of '' or port 0 tells the OS to use the default. """ msg = "getaddrinfo returns an empty list" @@ -296,6 +300,8 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT): sock = socket(af, socktype, proto) if timeout is not _GLOBAL_DEFAULT_TIMEOUT: sock.settimeout(timeout) + if source_address: + sock.bind(source_address) sock.connect(sa) return sock |