1

I have a web-application developed in Python and need to make calls to some underlying C++ code for computationally intensive tasks. I'm currently using the C-api integrated in python, but I want to fundamentally change my solution: I want the C++ code to directly receive HTTP requests from the front end and to do its computation separate of python. I also want to enable the user to ask the C++ code for data with GET requests etc. The C++ interfaces with a Postgres database (if that matters).

I'm wondering if any of you have some technology suggestions or resources to go peruse through?

2 Answers 2

4

There's a pretty long discussion about this topic here that'll probably leave you unsatisfied. Bottom line is that there's no single best practice for this kind of thing. Choices seem boil down to one of these approaches:

  • Run your C++ code behind a web server as an apache/ISAPI module or as a FastCGI server.
  • Run an embedded web server in your C++ process like Libmicrohttpd.
  • Use a more full-blown C++ web framework like Silicon (very new, looks pretty cool) or pion.
Sign up to request clarification or add additional context in comments.

2 Comments

Silicon looks really interesting. I did not know getting c++ to work with the web would require so much activation energy ;)
Yeah, C++ web development is not for the faint of heart. I had a similar project a couple of years ago where I had to expose a C API as a REST service and decided against taking this path--I ended up writing my web logic in Go and used cgo to handle the interop. It was pretty clean because Go was basically designed for this kind of stuff. So, unless you're intent of tackling a big project, I'd just punt and use one of the many straightforward Python REST frameworks out there since you obviously have the Python-to-C++ interop stuff figured out already.
1

For a RESTful API to work, all you really need to do is to have a program that responds to web requests. That can actually be achieved quite easily if you are willing to run basic CGI and you can use Apache's default settings to get you going immediately.

For example the following C program generated the output that follows.

#include <unistd.h>
#include <stdio.h>

extern char **environ;

void main (int argc, char* argv[]) {

    printf("Content-Type: text/html\n\n");

    printf("<html> <head>\n");
    printf("<title>Hello, world!</title>");
    printf("</head>\n");
    printf("<body>\n");
    printf("<h1>Hello, world!</h1>\n");

    printf("<ul>");
    int i = 0;
    while(environ[i]) {
        printf("<li>%s</li>\n", environ[i++]);
    }
    printf("</ul>");

    printf("</body> </html>\n");
}

Then I compiled the code to a file called sample which I put in /var/www/cgi-bin and then I called up http://localhost/cgi-bin/sample?a=100&b=test which produced the following (roughly):

Hello, world!

UNIQUE_ID=VlWFh2o2Wu4o73LCT4efZgAAAAk
HTTP_HOST=192.168.1.12
HTTP_CONNECTION=keep-alive
HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
HTTP_UPGRADE_INSECURE_REQUESTS=1
HTTP_USER_AGENT=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
HTTP_ACCEPT_ENCODING=gzip, deflate, sdch
HTTP_ACCEPT_LANGUAGE=en-GB,en-US;q=0.8,en;q=0.6
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
SERVER_SIGNATURE=
SERVER_SOFTWARE=Apache/2.4.6 (CentOS)
SERVER_NAME=192.168.1.12
SERVER_ADDR=192.168.1.12
SERVER_PORT=80
REMOTE_ADDR=192.168.1.100
DOCUMENT_ROOT=/var/www/html
REQUEST_SCHEME=http
CONTEXT_PREFIX=/cgi-bin/
CONTEXT_DOCUMENT_ROOT=/var/www/cgi-bin/
SERVER_ADMIN=root@localhost
SCRIPT_FILENAME=/var/www/cgi-bin/sample
REMOTE_PORT=58551
GATEWAY_INTERFACE=CGI/1.1
SERVER_PROTOCOL=HTTP/1.1
REQUEST_METHOD=GET
QUERY_STRING=a=100&b=test
REQUEST_URI=/cgi-bin/sample?a=100&b=test
SCRIPT_NAME=/cgi-bin/sample

That should at least get you up and going with the nitty-gritty. Then you can worry about tailoring your web config if necessary or preparing the code for a production environment.

Now its just a matter of extending that idea by writing it in C++ and using standard C++ libraries to do whatever you want and to respond to your requests in a RESTful way.

If you do plan on moving it into a production environment in the end then you ought to look into configuring Apache for FastCGI. That's a small project in itself, but it won't change the way you write your C++ code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.