Added a Python extension to send/receive file descriptors using libht.
authorFredrik Tolf <fredrik@dolda2000.com>
Thu, 21 Oct 2010 08:16:14 +0000 (10:16 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Thu, 21 Oct 2010 09:16:43 +0000 (11:16 +0200)
python/.gitignore
python/htp.c [new file with mode: 0644]
python/setup.py [new file with mode: 0755]

index 0d20b64..21e5002 100644 (file)
@@ -1 +1,3 @@
 *.pyc
+/build
+/ashd/htlib.so
diff --git a/python/htp.c b/python/htp.c
new file mode 100644 (file)
index 0000000..0405809
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+    ashd - A Sane HTTP Daemon
+    Copyright (C) 2008  Fredrik Tolf <fredrik@dolda2000.com>
+
+    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 <http://www.gnu.org/licenses/>.
+*/
+
+#include <Python.h>
+#include <errno.h>
+
+#include <utils.h>
+#include <proc.h>
+
+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 (executable)
index 0000000..117ab3f
--- /dev/null
@@ -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")