diff --git a/README.adoc b/README.adoc index 47748c9..f84b013 100644 --- a/README.adoc +++ b/README.adoc @@ -461,7 +461,7 @@ ROWID type oid === Functions -==== pg8000.connect(user, host='localhost', unix_sock=None, port=5432, database=None, password=None, ssl=None, timeout=None, application_name=None, tcp_keepalive=True) +==== pg8000.connect(user, host='localhost', source_address=None, unix_sock=None, port=5432, database=None, password=None, ssl=None, timeout=None, application_name=None, tcp_keepalive=True) Creates a connection to a PostgreSQL database. @@ -478,6 +478,11 @@ host:: parameter is necessary for TCP/IP connections. One of either `host` or `unix_sock` must be provided. The default is `localhost`. +source_address:: + The source IP address which initiates the connection to the PostgreSQL server. + This is an optional parameter, if not set the operation system will choose an + source address + unix_sock:: The path to the UNIX socket to access the database through, for example, `'/tmp/.s.PGSQL.5432'`. One of either `host` or `unix_sock` must be provided. @@ -1288,7 +1293,7 @@ Note that this version is not backward compatible with previous versions. 2 it's recommended to send text as unicode() objects. * Dropped and added support for Python versions. Now pg8000 supports - Python 2.7+ and Python 3.3+. + Python 2.7+ and Python 3.3+. * Dropped and added support for PostgreSQL versions. Now pg8000 supports PostgreSQL 9.1+. diff --git a/pg8000/__init__.py b/pg8000/__init__.py index 56a06c6..d5e0cd4 100644 --- a/pg8000/__init__.py +++ b/pg8000/__init__.py @@ -41,13 +41,14 @@ def connect( - user, host='localhost', unix_sock=None, port=5432, database=None, - password=None, ssl=None, timeout=None, application_name=None, - max_prepared_statements=1000, tcp_keepalive=True): + user, host='localhost', source_address=None, unix_sock=None, port=5432, + database=None, password=None, ssl=None, timeout=None, + application_name=None, max_prepared_statements=1000, + tcp_keepalive=True): return Connection( - user, host, unix_sock, port, database, password, ssl, timeout, - application_name, max_prepared_statements, tcp_keepalive) + user, host, source_address, unix_sock, port, database, password, ssl, + timeout, application_name, max_prepared_statements, tcp_keepalive) apilevel = "2.0" diff --git a/pg8000/core.py b/pg8000/core.py index aabb65f..85e7e21 100644 --- a/pg8000/core.py +++ b/pg8000/core.py @@ -1118,8 +1118,9 @@ def _getError(self, error): return error def __init__( - self, user, host, unix_sock, port, database, password, ssl, - timeout, application_name, max_prepared_statements, tcp_keepalive): + self, user, host, source_address, unix_sock, port, database, + password, ssl, timeout, application_name, max_prepared_statements, + tcp_keepalive): self._client_encoding = "utf8" self._commands_with_count = ( b"INSERT", b"DELETE", b"UPDATE", b"MOVE", b"FETCH", b"COPY", @@ -1151,6 +1152,8 @@ def __init__( try: if unix_sock is None and host is not None: self._usock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + if source_address is not None: + self._usock.bind((source_address, 0)) elif unix_sock is not None: if not hasattr(socket, "AF_UNIX"): raise InterfaceError( diff --git a/versioneer.py b/versioneer.py index c010f63..f188768 100644 --- a/versioneer.py +++ b/versioneer.py @@ -1,5 +1,5 @@ -# Version: 0.15 +# Version: 0.16 """ The Versioneer