Add bind_address option (#529)
Allow connecting to the DB from a specific network interface
This commit is contained in:
committed by
INADA Naoki
parent
6c9d31a946
commit
755dfdc2e1
@@ -534,7 +534,8 @@ class Connection(object):
|
|||||||
compress=None, named_pipe=None, no_delay=None,
|
compress=None, named_pipe=None, no_delay=None,
|
||||||
autocommit=False, db=None, passwd=None, local_infile=False,
|
autocommit=False, db=None, passwd=None, local_infile=False,
|
||||||
max_allowed_packet=16*1024*1024, defer_connect=False,
|
max_allowed_packet=16*1024*1024, defer_connect=False,
|
||||||
auth_plugin_map={}, read_timeout=None, write_timeout=None):
|
auth_plugin_map={}, read_timeout=None, write_timeout=None,
|
||||||
|
bind_address=None):
|
||||||
"""
|
"""
|
||||||
Establish a connection to the MySQL database. Accepts several
|
Establish a connection to the MySQL database. Accepts several
|
||||||
arguments:
|
arguments:
|
||||||
@@ -544,6 +545,9 @@ class Connection(object):
|
|||||||
password: Password to use.
|
password: Password to use.
|
||||||
database: Database to use, None to not use a particular one.
|
database: Database to use, None to not use a particular one.
|
||||||
port: MySQL port to use, default is usually OK. (default: 3306)
|
port: MySQL port to use, default is usually OK. (default: 3306)
|
||||||
|
bind_address: When the client has multiple network interfaces, specify
|
||||||
|
the interface from which to connect to the host. Argument can be
|
||||||
|
a hostname or an IP address.
|
||||||
unix_socket: Optionally, you can use a unix socket rather than TCP/IP.
|
unix_socket: Optionally, you can use a unix socket rather than TCP/IP.
|
||||||
charset: Charset you want to use.
|
charset: Charset you want to use.
|
||||||
sql_mode: Default SQL_MODE to use.
|
sql_mode: Default SQL_MODE to use.
|
||||||
@@ -632,6 +636,7 @@ class Connection(object):
|
|||||||
database = _config("database", database)
|
database = _config("database", database)
|
||||||
unix_socket = _config("socket", unix_socket)
|
unix_socket = _config("socket", unix_socket)
|
||||||
port = int(_config("port", port))
|
port = int(_config("port", port))
|
||||||
|
bind_address = _config("bind-address", bind_address)
|
||||||
charset = _config("default-character-set", charset)
|
charset = _config("default-character-set", charset)
|
||||||
|
|
||||||
self.host = host or "localhost"
|
self.host = host or "localhost"
|
||||||
@@ -640,6 +645,7 @@ class Connection(object):
|
|||||||
self.password = password or ""
|
self.password = password or ""
|
||||||
self.db = database
|
self.db = database
|
||||||
self.unix_socket = unix_socket
|
self.unix_socket = unix_socket
|
||||||
|
self.bind_address = bind_address
|
||||||
if read_timeout is not None and read_timeout <= 0:
|
if read_timeout is not None and read_timeout <= 0:
|
||||||
raise ValueError("read_timeout should be >= 0")
|
raise ValueError("read_timeout should be >= 0")
|
||||||
self._read_timeout = read_timeout
|
self._read_timeout = read_timeout
|
||||||
@@ -884,10 +890,14 @@ class Connection(object):
|
|||||||
self.host_info = "Localhost via UNIX socket"
|
self.host_info = "Localhost via UNIX socket"
|
||||||
if DEBUG: print('connected using unix_socket')
|
if DEBUG: print('connected using unix_socket')
|
||||||
else:
|
else:
|
||||||
|
kwargs = {}
|
||||||
|
if self.bind_address is not None:
|
||||||
|
kwargs['source_address'] = (self.bind_address, 0)
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
sock = socket.create_connection(
|
sock = socket.create_connection(
|
||||||
(self.host, self.port), self.connect_timeout)
|
(self.host, self.port), self.connect_timeout,
|
||||||
|
**kwargs)
|
||||||
break
|
break
|
||||||
except (OSError, IOError) as e:
|
except (OSError, IOError) as e:
|
||||||
if e.errno == errno.EINTR:
|
if e.errno == errno.EINTR:
|
||||||
|
|||||||
Reference in New Issue
Block a user