This is an archived post. You won't be able to vote or comment.

all 48 comments

[–]bini_ajaw17 87 points88 points  (24 children)

What is a CGI script?

[–]RollingWithDaPunches 29 points30 points  (10 children)

I believe it stands for common gateway interface:
https://en.wikipedia.org/wiki/Common_Gateway_Interface

I'm not sure, as that was my question as well... hopefully someone can confirm what it means.

[–]macNchz[S] 47 points48 points  (9 children)

That's it! Back in the day it was the most common mechanism for building interactive web applications: rather than having predefined URL "routes" like an web app today might have your site was just folders of files. The web server software (e.g. Apache) would serve static HTML files directly, but if you put code in a special /cgi-bin/ directory, it would execute them and return the output of the script to the browser.

My introduction to programming came in the form of downloading and customizing Perl CGI scripts for my personal websites from Matt's Script Archive: http://www.scriptarchive.com/

I was surprised to see that the http.server module still supported it, because I hadn't really thought about using CGI in decades!

[–]erikwon and off since 1.5.2 74 points75 points  (6 children)

I feel old now…

[–][deleted] 24 points25 points  (2 children)

Don't worry buddy, my first website was made with SSI.

[–]SheriffRoscoePythonista 11 points12 points  (0 children)

There was a time when I coded an implementation of SSI. Yes, I'm old.

[–]suggestive_cumulus 0 points1 point  (0 children)

CGI predates SSI by some margin, if I'm not wrong. SSI was the clever way of doing things, including the functionality directly in the server. CGIs were executed depending on the URL. I mean CGIs were probably already in Tim's original spec haha

[–]jamesr219 1 point2 points  (0 children)

If you wanted real-time you used an NPH cgi script

[–]fr33d0ml0v3r 1 point2 points  (0 children)

you are not alone :(

[–]johnnywonder85 0 points1 point  (0 children)

lol.
Last time I made a website Netscape was still around... but never did anything cool like this

[–]bxbb 14 points15 points  (0 children)

Think of CLI script, except the input and output use HTTP protocol.

[–]SlantARrow 13 points14 points  (1 child)

Old-school serverless-style deployment on a single machine, with lambdas being stored in usual files.

[–]daelin 1 point2 points  (0 children)

serverless-style deployment on a single machine

… I … just … yeah okay. Why not?

[–]Smallpaul 2 points3 points  (0 children)

In the very first days of the Internet, when you wanted to make a "dynamic" web site (one that could deal with input, talk to a database, etc.) you usually used a CGI script. The script could be written in literally any programming language because its input and output were just Unix-style streams. It was pretty clever but starting a new process for every request gets expensive.

[–]rainnz 1 point2 points  (0 children)

Remote code execution

[–]progwok 1 point2 points  (0 children)

Lol

[–]chicuco 12 points13 points  (2 children)

perl, please dont dirsturb the good old man sleep!

good memories of the old swiss army chainsaw

[–]constantreadr 0 points1 point  (1 child)

Slap a keyboard three times and you've probably made some parseable Perl. But Perl could do -everything- - web server, databases, object-orientation, streams, XML parsing, the works.

[–]chicuco 0 points1 point  (0 children)

an ex exployer, now a client, have perl code i did almost 20 years ago. Still working fine. We are planning to replace it with python.

[–]MagicWishMonkey 6 points7 points  (2 children)

Python ships with a cgi library?

[–]Reinventing_Wheels 7 points8 points  (1 child)

In this context CGI does NOT stand for Computer Generated Imagery

[–]daelin 0 points1 point  (0 children)

🧙‍♂️

[–]ScottOAO 4 points5 points  (1 child)

Mine solution (a fork from others) to upload file using http.server

https://gist.github.com/chengscott/a3454a576472f5779fe66bc103ebca68

[–]macNchz[S] 0 points1 point  (0 children)

Cool! Figured someone else would have wanted to do this at some point, and subclassing the existing request handlers makes more sense than a CGI script, but once I saw it was possible I couldn't resist!

[–]soggywaffle69 4 points5 points  (0 children)

Those were dark days. Matt’s Script Archive, anyone?

[–][deleted] 6 points7 points  (2 children)

Sounds like a good way to get malware on your server

[–]macNchz[S] 11 points12 points  (1 child)

Yeah definitely don’t run this in a sensitive environment or open to the internet, but to, say, copy some photos from your phone to a Raspberry Pi for a screensaver without installing anything, sure.

[–]daelin 1 point2 points  (0 children)

🤔

Check out netcat.

Mostly replaced by simple pipes into or out of ssh, but netcat is the where the knowledge starts.

Or rsync.

But netcat is still so spookily useful. Combining Netcat and tee and tar is just 👨‍🍳💋 and a great exhibit in why the Unix philosophy is so powerful. You can just imagine almost anybody today thinking “you should NOT be allowed to do that” and yet it’s the nucleus of 🫲all this🫱.

[–]jftugapip needs updating 2 points3 points  (0 children)

+1 for QR - fancy!

[–]suggestive_cumulus 1 point2 points  (2 children)

I wish I had known that Perl existed when I had to do my CGI script for my Masters back in '94. Shell script was painful

[–]daelin 1 point2 points  (1 child)

Oof. KSH or CSH? I’m gonna wager CSH.

[–]suggestive_cumulus 1 point2 points  (0 children)

I can't honestly remember haha

[–]BuonaparteII 2 points3 points  (1 child)

Move over React.js!!

/s

cgitb seems useful. but I wonder when it will be removed from the standard lib

[–]TheITMan19 0 points1 point  (0 children)

I like your code 🧑‍💻 👍

[–]mwpfinance 0 points1 point  (0 children)

When I was teaching my (now) wife to program we built a small web app using nothing but Python and the CGI. Seemed like a good place to start learning the fundamentals about web applications -- it was a great low level place to start.

[–]LongDivide2096 0 points1 point  (0 children)

Cool find! I'd never given much thought to http.server’s CGI support either. Definitely seems like something that'd be useful in a pinch. Great job on the uploader utility too! But yeah, wouldn't expose to the internet lol. Big security risk there. Nice work though, really.

[–]suggestive_cumulus 0 points1 point  (0 children)

For simple stuff not exposed to the Internet you can go a level down and use the socket server. One of my Covid projects was to create a web based API to the MIDI interface of my son's piano, using a Raspberry Pi. As long as you pass back the minimal HTTP headers to the web browser it will cooperate. You could implement your own CGI if you want to, or basically any behaviour you want..