From: Fredrik Tolf Date: Thu, 21 Oct 2010 08:16:14 +0000 (+0200) Subject: Added a Python extension to send/receive file descriptors using libht. X-Git-Tag: 0.4~19 X-Git-Url: http://www.dolda2000.com/gitweb/?p=ashd.git;a=commitdiff_plain;h=4942dacd8281b9095468148b0df84af9e0aaaddc Added a Python extension to send/receive file descriptors using libht. --- diff --git a/python/.gitignore b/python/.gitignore index 0d20b64..21e5002 100644 --- a/python/.gitignore +++ b/python/.gitignore @@ -1 +1,3 @@ *.pyc +/build +/ashd/htlib.so diff --git a/python/htp.c b/python/htp.c new file mode 100644 index 0000000..0405809 --- /dev/null +++ b/python/htp.c @@ -0,0 +1,73 @@ +/* + ashd - A Sane HTTP Daemon + Copyright (C) 2008 Fredrik Tolf + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include + +#include +#include + +static PyObject *p_recvfd(PyObject *self, PyObject *args) +{ + int fd, ret; + char *data; + size_t dlen; + PyObject *ro; + + fd = 0; + if(!PyArg_ParseTuple(args, "|i", &fd)) + return(NULL); + if((ret = recvfd(fd, &data, &dlen)) < 0) { + if(errno == 0) + return(Py_BuildValue("OO", Py_None, Py_None)); + PyErr_SetFromErrno(PyExc_OSError); + return(NULL); + } + ro = Py_BuildValue("Ni", PyString_FromStringAndSize(data, dlen), ret); + free(data); + return(ro); +} + +static PyObject *p_sendfd(PyObject *self, PyObject *args) +{ + int sock, fd; + PyObject *data; + + if(!PyArg_ParseTuple(args, "iiO", &sock, &fd, &data)) + return(NULL); + if(!PyString_Check(data)) { + PyErr_SetString(PyExc_TypeError, "datagram must be a string"); + return(NULL); + } + if(sendfd(sock, fd, PyString_AsString(data), PyString_Size(data)) < 0) { + PyErr_SetFromErrno(PyExc_OSError); + return(NULL); + } + Py_RETURN_NONE; +} + +static PyMethodDef methods[] = { + {"recvfd", p_recvfd, METH_VARARGS, "Receive a datagram and a file descriptor"}, + {"sendfd", p_sendfd, METH_VARARGS, "Send a datagram and a file descriptor"}, + {NULL, NULL, 0, NULL} +}; + +PyMODINIT_FUNC inithtlib(void) +{ + Py_InitModule("ashd.htlib", methods); +} diff --git a/python/setup.py b/python/setup.py new file mode 100755 index 0000000..117ab3f --- /dev/null +++ b/python/setup.py @@ -0,0 +1,18 @@ +#!/usr/bin/python + +from distutils.core import setup, Extension + +htlib = Extension("ashd.htlib", ["htp.c"], + libraries = ["ht"], + library_dirs = ["../lib/"], + include_dirs = ["../lib/"]) + +setup(name = "ashd-py", + version = "0.1", + description = "Python module for handling ashd requests", + author = "Fredrik Tolf", + author_email = "fredrik@dolda2000.com", + url = "http://www.dolda2000.com/~fredrik/ashd/", + ext_modules = [htlib], + packages = ["ashd"], + license = "GPL-3")