summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorGregory P. Smith <greg@mad-scientist.com>2010-01-03 02:06:07 +0000
committerGregory P. Smith <greg@mad-scientist.com>2010-01-03 02:06:07 +0000
commit57512d8e66e60258f6f63db86a5c5a3ea81f4e20 (patch)
treef82743f741065c7ee6d25084c77e96a858afe549 /Lib
parent38aa5daa188d8b6c75e4ec235b08b43866b3721c (diff)
downloadcpython-57512d8e66e60258f6f63db86a5c5a3ea81f4e20.tar.gz
issue3972: HTTPConnection and HTTPSConnection now support a
source_address parameter. Also cleans up an annotation in the socket documentation.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/httplib.py14
-rw-r--r--Lib/test/test_httplib.py37
2 files changed, 44 insertions, 7 deletions
diff --git a/Lib/httplib.py b/Lib/httplib.py
index 39acd1c04b..c5e600c709 100644
--- a/Lib/httplib.py
+++ b/Lib/httplib.py
@@ -658,8 +658,9 @@ class HTTPConnection:
strict = 0
def __init__(self, host, port=None, strict=None,
- timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
+ timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None):
self.timeout = timeout
+ self.source_address = source_address
self.sock = None
self._buffer = []
self.__response = None
@@ -728,7 +729,7 @@ class HTTPConnection:
def connect(self):
"""Connect to the host and port specified in __init__."""
self.sock = socket.create_connection((self.host,self.port),
- self.timeout)
+ self.timeout, self.source_address)
if self._tunnel_host:
self._tunnel()
@@ -1133,15 +1134,18 @@ else:
default_port = HTTPS_PORT
def __init__(self, host, port=None, key_file=None, cert_file=None,
- strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
- HTTPConnection.__init__(self, host, port, strict, timeout)
+ strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
+ source_address=None):
+ HTTPConnection.__init__(self, host, port, strict, timeout,
+ source_address)
self.key_file = key_file
self.cert_file = cert_file
def connect(self):
"Connect to a host on a given (SSL) port."
- sock = socket.create_connection((self.host, self.port), self.timeout)
+ sock = socket.create_connection((self.host, self.port),
+ self.timeout, self.source_address)
if self._tunnel_host:
self.sock = sock
self._tunnel()
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index cd54323f1a..c0c12e7122 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -3,7 +3,8 @@ import httplib
import StringIO
import socket
-from unittest import TestCase
+import unittest
+TestCase = unittest.TestCase
from test import test_support
@@ -237,6 +238,38 @@ class OfflineTest(TestCase):
def test_responses(self):
self.assertEquals(httplib.responses[httplib.NOT_FOUND], "Not Found")
+
+class SourceAddressTest(TestCase):
+ def setUp(self):
+ self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.port = test_support.bind_port(self.serv)
+ self.source_port = test_support.find_unused_port()
+ self.serv.listen(5)
+ self.conn = None
+
+ def tearDown(self):
+ if self.conn:
+ self.conn.close()
+ self.conn = None
+ self.serv.close()
+ self.serv = None
+
+ def testHTTPConnectionSourceAddress(self):
+ self.conn = httplib.HTTPConnection(HOST, self.port,
+ source_address=('', self.source_port))
+ self.conn.connect()
+ self.assertEqual(self.conn.sock.getsockname()[1], self.source_port)
+
+ @unittest.skipIf(not hasattr(httplib, 'HTTPSConnection'),
+ 'httplib.HTTPSConnection not defined')
+ def testHTTPSConnectionSourceAddress(self):
+ self.conn = httplib.HTTPSConnection(HOST, self.port,
+ source_address=('', self.source_port))
+ # We don't test anything here other the constructor not barfing as
+ # this code doesn't deal with setting up an active running SSL server
+ # for an ssl_wrapped connect() to actually return from.
+
+
class TimeoutTest(TestCase):
PORT = None
@@ -294,7 +327,7 @@ class HTTPSTimeoutTest(TestCase):
def test_main(verbose=None):
test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
- HTTPSTimeoutTest)
+ HTTPSTimeoutTest, SourceAddressTest)
if __name__ == '__main__':
test_main()