Determine preferable local address to connect through.

If listen contains '*' or 0.0.0.0 - connect via localhost
In all other cases pick the first one.
This commit is contained in:
Alexander Kukushkin
2015-06-01 17:00:15 +02:00
parent efccd777b2
commit bacd05d99e
2 changed files with 9 additions and 3 deletions
+8 -2
View File
@@ -57,8 +57,14 @@ class Postgresql:
self.members = [] # list of already existing replication slots
def get_local_address(self):
# TODO: try to get unix_socket_directory from postmaster.pid
return self.listen_addresses.split(',')[0].strip() + ':' + self.port
listen_addresses = self.listen_addresses.split(',')
local_address = listen_addresses[0].strip() # take first address from listen_addresses
for la in listen_addresses:
if la.strip() in ['*', '0.0.0.0']: # we are listening on *
local_address = 'localhost' # connection via localhost is preferred
break
return local_address + ':' + self.port
def connection(self):
if not self._connection or self._connection.closed != 0:
+1 -1
View File
@@ -91,7 +91,7 @@ class TestPostgresql(unittest.TestCase):
def set_up(self):
subprocess.call = subprocess_call
self.p = Postgresql({'name': 'test0', 'data_dir': 'data/test0', 'listen': '127.0.0.1, 127.0.0.2:5432',
self.p = Postgresql({'name': 'test0', 'data_dir': 'data/test0', 'listen': '127.0.0.1, *:5432',
'connect_address': '127.0.0.2:5432',
'pg_hba': [{'type': 'hostssl', 'database': 'all', 'user': 'all', 'address': '0.0.0.0/0',
'method': 'md5'}, {'type': 'host', 'database': 'all', 'user': 'all',