1
1
mirror of https://github.com/psycopg/psycopg2.git synced 2025-04-15 14:01:59 +03:00

Add connection.get_host_addr()

This commit is contained in:
Emanuele Giaquinta 2024-03-11 12:38:35 +02:00
parent a971c11d50
commit 555e9f4489
3 changed files with 29 additions and 0 deletions

View File

@ -910,6 +910,20 @@ The ``connection`` class
.. versionadded:: 2.7
.. index::
pair: Connection; Parameters
.. method:: get_host_addr()
Get the server IP address of the active connection.
.. seealso:: libpq docs for `PQhostaddr()`__ for details.
.. __: https://www.postgresql.org/docs/current/libpq-status.html#LIBPQ-PQHOSTADDR
.. versionadded:: 2.9.10
.. testcode::
:hide:

View File

@ -947,6 +947,16 @@ exit:
#endif
}
#define psyco_conn_get_host_addr_doc \
"get_host_addr() -- Get the server IP address of the active connection."
static PyObject *
psyco_conn_get_host_addr(connectionObject *self, PyObject *dummy)
{
EXC_IF_CONN_CLOSED(self);
return Text_FromUTF8(PQhostaddr(self->pgconn));
}
/* lobject method - allocate a new lobject */
@ -1220,6 +1230,8 @@ static struct PyMethodDef connectionObject_methods[] = {
METH_VARARGS, psyco_conn_get_parameter_status_doc},
{"get_dsn_parameters", (PyCFunction)psyco_conn_get_dsn_parameters,
METH_NOARGS, psyco_conn_get_dsn_parameters_doc},
{"get_host_addr", (PyCFunction)psyco_conn_get_host_addr,
METH_NOARGS, psyco_conn_get_host_addr_doc},
{"get_backend_pid", (PyCFunction)psyco_conn_get_backend_pid,
METH_NOARGS, psyco_conn_get_backend_pid_doc},
{"lobject", (PyCFunction)psyco_conn_lobject,

View File

@ -1826,6 +1826,9 @@ class TestConnectionInfo(ConnectingTestCase):
self.assertEqual(d['dbname'], dbname) # the only param we can check reliably
self.assert_('password' not in d, d)
def test_host_addr(self):
self.assertEqual(self.conn.get_host_addr(), "")
def test_status(self):
self.assertEqual(self.conn.info.status, 0)
self.assertEqual(self.bconn.info.status, 1)