Apache不会运行Py_Initialize();功能

时间:2011-12-13 17:11:59

标签: python c apache2

如何将python函数运行到Apache模块中?

#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "python2.7/Python.h"
static char* a(){
    Py_Initialize();
    PyRun_SimpleString("f = open('/tmp/workfile', 'w+')")
                        "f.write('0123456789abcdef')");
    Py_Finalize();
    return "Goog_Job\t";
}

/* The sample content handler */
static int mor_handler(request_rec *r)
{
    if (strcmp(r->handler, "mor")) {
        return DECLINED;
    }
    r->content_type = "text/html";         
    if (!r->header_only) {
        char *d;
        d = a();
        ap_rputs(d, r);
        ap_rputs("The sample page from mod_mor.c\n", r);}
        return OK;
    }
}

static void mor_register_hooks(apr_pool_t *p)
{
    ap_hook_handler(mor_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA mor_module = {
    STANDARD20_MODULE_STUFF, 
    NULL,                  /* create per-dir    config structures */
    NULL,                  /* merge  per-dir    config structures */
    NULL,                  /* create per-server config structures */
    NULL,                  /* merge  per-server config structures */
    NULL,                  /* table of config file commands       */
    mor_register_hooks  /* register hooks                      */
};

虽然我有

,但我的浏览器中没有http://localhost/mor显示任何内容
LoadModule mor_module         /usr/lib/apache2/modules/mod_mor.so
<Location /mor>
    SetHandler mor
</Location>

1 个答案:

答案 0 :(得分:2)

这是如此艰难的方式!!

使用python和mod_wsgi。这很容易。 试试这个指南。 http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide