c2w: Convert Any CLI Program to a Web Service

DONG Yuxuan @ Jun 13, 2020 CST

Many experienced hackers have great ideas about technology like a cool algorithm. However, they don’t like to work on the code they think is trivial, for example, making the algorithm a Web service or writing a GUI for it. This will increase the threshold to enjoy the result and make less people understand their work, thus I wrote the c2w utility to convert any CLI program to a web service.

You could install it by pip install c2w.

For example, you wrote a very advanced English-to-Chinese dict program entocn.

% entocn hello 

You want to share with as many people as possible. If you delivery your program as a CLI program, you can get feedbacks from only hackers. With c2w, you could build a Web service with one line.

% c2w -H entocn

You get entocn online on the port 8000 and people on the Internet can use it by visiting http://yourhost:8000/?hello and they get the corresponding Chinese word of hello in their browsers.

Tools like c2w often provide a standalone Web server but you may want to work with a mature Web server like Apache HTTPd. c2w provides CGI mode which can convert your program to a CGI script.


c2w --cgi entocn

Put the above script in the cgi-bin directory of your Web server and you get a CGI script which can translate English words to Chinese words.

c2w parses the query string, splits it by &, decodes the split list and sends them as the command line arguments to the target program.

The request body will be directly sent to the standard input of the target body and the standard output of the target program will be redirected to the browser.

Call c2w --help to see the usage. Visit the Github repository of c2w for more information.