Amrith Krishna
Amrith Krishna

Reputation: 2851

Passing data from Server in C language to a python script

I am developing a prototype web application. I am using a toy http server which I have developed myself and it is written in C. The server needs to obtain text data from html form submitted and for furthur processing should pass it to a script which I have written in python. I know the following methods to achieve the same. Also once the processing is done the script has to return the data to server.

  1. Save the data in a file and let the python script access it.
  2. Calling the system or exec family of functions from within the Server code.
  3. I have heard of CGI where a socket is used on both sides. I know how to do the same in my python script. But I am not able to see how the same is done in C server side.

Normally this is abstracted out as most people use apache or nginx as theor service. How can CGI be achieved in Server code?

Upvotes: 0

Views: 114

Answers (1)

Mark Nunberg
Mark Nunberg

Reputation: 3701

CGI is covered here: https://www.rfc-editor.org/rfc/rfc3875

By 'CGI over sockets' you probably mean FastCGI which is a bit different (and involves a long running process listening on a Unix socket and sending back responses). Classical CGI involves spawning a short lived process which then receives the request parameters via environment variables (set this from your server via setenv().

Request data is sent to the spawned process via stdin (e.g. in Python, sys.stdin). The output response is then written (with headers and all) via stdout.

CGI is very simple and this is why it was so quick to be adopted by a high number of languages -- the learning curve to implementing CGI was rather quick.

FastCGI is similar to CGI only at the script language layer (i.e. the programming interface remains somewhat similar); however the actual mechanics are rather different (you need to serialize the information over Unix sockets).

Upvotes: 1

Related Questions