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.
printfsends its output, so it looks like that's working for you. The issue might be that Python is sending its output somewhere else, but my guess would be that the embedded Python interpreter is not generating output at all.printffunction, as well as the other functions in the stdio header.