|
Richard Barrett
R.Barrett at ftel.co.uk
Wed Aug 23 11:30:18 EST 2000
I've started working with mod_python-2.4.1; good stuff.
I wanted to access the local_addr and remote_addr fields of the
conn_rec struct but access to the fields is not supported via the
connection object by mod_python.so
Accordingly I've added some code to mod_python.c to return the
contents of these fields. The code is a minor variation of functions
'borrowed' from socketmodule.c in the standard Python 1.5.2
distribution. The python object returned is the same as that returned
by the getpeername and getsockname member functions on standard
socket objects. I would have called functions makeipaddr and
makesockaddr in socketmodule.so instead of adding copies to
mod_python but they are declared as static in socketmodule.c and
hence are unavailable.
If there is a better way of doing this or anything I've said above is
rubbish I would appreciate some (polite) feedback. I don't claim any
particular expertise with C or python extensions so some reader may
find fault with this stuff. But it works for me so far.
The diff for my changes to mod_python.c follow:
-----------------------------------------------------
91a92,94
> /* Other headers */
> #include <sys/socket.h>
>
928a932,954
> static PyObject *
> mymakeipaddr(struct sockaddr_in *addr)
> {
> long x = ntohl(addr->sin_addr.s_addr);
> char buf[100];
> sprintf(buf, "%d.%d.%d.%d",
> (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
> (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
> return PyString_FromString(buf);
> }
>
> static PyObject *
> mymakesockaddr(struct sockaddr_in *addr)
> {
> PyObject *addrobj = mymakeipaddr(addr);
> PyObject *ret = NULL;
> if (addrobj) {
> ret = Py_BuildValue("Oi", addrobj, ntohs(addr->sin_port));
> Py_DECREF(addrobj);
> }
> return ret;
> }
>
968a995,1000
> else if (strcmp(name, "local_addr") == 0) {
> return mymakesockaddr(&(self->conn->local_addr));
> }
> else if (strcmp(name, "remote_addr") == 0) {
> return mymakesockaddr(&(self->conn->remote_addr));
> }
------------------------------------------------------------------
Richard Barrett, PostPoint 27, e-mail:r.barrett at ftel.co.uk
Fujitsu Telecommunications Europe Ltd, tel: (44) 121 717 6337
Solihull Parkway, Birmingham Business Park, B37 7YU, England
"Democracy is two wolves and a lamb voting on what to have for
lunch. Liberty is a well armed lamb contesting the vote."
Benjamin Franklin, 1759
------------------------------------------------------------------
|