|
| 1 | +From 755dfdc2e16f2f7f5fa6669cb81d0ec3118ec203 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Damien Ciabrini <damien.ciabrini@gmail.com> |
| 3 | +Date: Wed, 16 Nov 2016 13:11:03 +0100 |
| 4 | +Subject: [PATCH] Add bind_address option (#529) |
| 5 | + |
| 6 | +Allow connecting to the DB from a specific network interface |
| 7 | +--- |
| 8 | + pymysql/connections.py | 14 ++++++++++++-- |
| 9 | + 1 file changed, 12 insertions(+), 2 deletions(-) |
| 10 | + |
| 11 | +diff --git a/pymysql/connections.py b/pymysql/connections.py |
| 12 | +index d5e39a1..2884cdc 100644 |
| 13 | +--- a/pymysql/connections.py |
| 14 | ++++ b/pymysql/connections.py |
| 15 | +@@ -534,7 +534,8 @@ class Connection(object): |
| 16 | + compress=None, named_pipe=None, no_delay=None, |
| 17 | + autocommit=False, db=None, passwd=None, local_infile=False, |
| 18 | + max_allowed_packet=16*1024*1024, defer_connect=False, |
| 19 | +- auth_plugin_map={}, read_timeout=None, write_timeout=None): |
| 20 | ++ auth_plugin_map={}, read_timeout=None, write_timeout=None, |
| 21 | ++ bind_address=None): |
| 22 | + """ |
| 23 | + Establish a connection to the MySQL database. Accepts several |
| 24 | + arguments: |
| 25 | +@@ -544,6 +545,9 @@ class Connection(object): |
| 26 | + password: Password to use. |
| 27 | + database: Database to use, None to not use a particular one. |
| 28 | + port: MySQL port to use, default is usually OK. (default: 3306) |
| 29 | ++ bind_address: When the client has multiple network interfaces, specify |
| 30 | ++ the interface from which to connect to the host. Argument can be |
| 31 | ++ a hostname or an IP address. |
| 32 | + unix_socket: Optionally, you can use a unix socket rather than TCP/IP. |
| 33 | + charset: Charset you want to use. |
| 34 | + sql_mode: Default SQL_MODE to use. |
| 35 | +@@ -632,6 +636,7 @@ class Connection(object): |
| 36 | + database = _config("database", database) |
| 37 | + unix_socket = _config("socket", unix_socket) |
| 38 | + port = int(_config("port", port)) |
| 39 | ++ bind_address = _config("bind-address", bind_address) |
| 40 | + charset = _config("default-character-set", charset) |
| 41 | + |
| 42 | + self.host = host or "localhost" |
| 43 | +@@ -640,6 +645,7 @@ class Connection(object): |
| 44 | + self.password = password or "" |
| 45 | + self.db = database |
| 46 | + self.unix_socket = unix_socket |
| 47 | ++ self.bind_address = bind_address |
| 48 | + if read_timeout is not None and read_timeout <= 0: |
| 49 | + raise ValueError("read_timeout should be >= 0") |
| 50 | + self._read_timeout = read_timeout |
| 51 | +@@ -884,10 +890,14 @@ class Connection(object): |
| 52 | + self.host_info = "Localhost via UNIX socket" |
| 53 | + if DEBUG: print('connected using unix_socket') |
| 54 | + else: |
| 55 | ++ kwargs = {} |
| 56 | ++ if self.bind_address is not None: |
| 57 | ++ kwargs['source_address'] = (self.bind_address, 0) |
| 58 | + while True: |
| 59 | + try: |
| 60 | + sock = socket.create_connection( |
| 61 | +- (self.host, self.port), self.connect_timeout) |
| 62 | ++ (self.host, self.port), self.connect_timeout, |
| 63 | ++ **kwargs) |
| 64 | + break |
| 65 | + except (OSError, IOError) as e: |
| 66 | + if e.errno == errno.EINTR: |
| 67 | +-- |
| 68 | +2.5.5 |
| 69 | + |
0 commit comments