pepoluan
pepoluan

Reputation: 6780

Python: emulate the functionality of "cgi-fcgi" program

I need to periodically pull status information from PHP-FPM. Currently I just parse the output of the following script:

export SCRIPT_NAME=/status
export SCRIPT_FILENAME=/status
export REQUEST_METHOD=GET
/usr/bin/cgi-fcgi -bind -connect /tmp/php5-fpm.sock

However, if possible, I'd like my Python program to actually do the cgi-fcgi stuff on its own.

I've tried searching for how Python can invoke CGI/FastCGI; unfortunately, all docs I found always talk about how to invoke a Python program via CGI/FastCGI. That is, Python on the 'server' side.

So, how do I implement a CGI/FastCGI 'client' on Python?

(Note that the cgi-fcgi allows direct access to the CGI/FastCGI listener; that's what I'm looking for)

Upvotes: 0

Views: 710

Answers (1)

pepoluan
pepoluan

Reputation: 6780

I've voted as duplicate of this question.

Still, I want to document my solution:

  1. I use a modified fcgi_app module from flup (namely, flup.client.fcgi_app)
  2. Modification done like in the linked question, but I used a 'pre-made' solution from this Gist on GitHub. It's simple, and it doesn't seem to have any dependencies outside of standard modules.
  3. Invocation as simple as the following:

    # "flup_fcgi_client.py" is the modified flup.client.fcgi_app module
    # located in the same directory
    import flup_fcgi_client as fcgi_client
    fcgi = fcgi_client.FCGIApp(connect='/path/to/socket')
    script = '/status'
    query = 'json'
    env = {
        'SCRIPT_NAME': script,
        'SCRIPT_FILENAME': script,
        'QUERY_STRING': query,
        'REQUEST_METHOD': 'GET'}
    
    code, headers, out, err = fcgi(env)
    
    # Handle return values here...
    

Upvotes: 1

Related Questions