I am using the Python C library and FastCGI to run code in a file and return the output to NGINX. The problem is PyRun_SimpleString
outputs to stdout
, which is fine for a CLI, but is ignored by FastCGI.
The code I tried was this:
#include <Python.h>
#include <string.h>
#include <fcgi_stdio.h>
int main() {
Py_Initialize();
while (FCGI_Accept() >= 0) {
printf("Content-type: text/plain\r\n\r\n");
PyRun_SimpleString("print('hey')");
}
Py_Finalize();
return 0;
}
My NGINX config:
location / {
fastcgi_pass unix:/tmp/sock-fcgi.sock;
include fastcgi_params;
fastcgi_param REQUEST_URI $uri;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
The returned value (from both curling the NGINX server and cgi-fcgi -connect /tmp/sock-fcgi.sock ./COMPILED_FILE
):
Content-type: text/plain
What I was expecting:
Content-type: text/plain
hey
EDIT: Running COMPILED_FILE
without cgi-fcgi
as you would run any other C programme outputs the expected values.