Saturday, July 01, 2017

Retro Coding

:: talks about CGI in the client / server context ::

Retro Coding is where one deliberately dials back to an earlier point in time and uses tools at that time considered cutting edge, but since abandoned.

A case in point:  "Common Gateway Interface" or "CGI" programming. "Computer-generated Imagery" is the more common decoding; a name collision (just get clear on the namespace, for less cognitive dissonance).

In Python, we import the cgi module primarily for access to FieldStorage, an object that plays the role of sys.argv in some ways, the latter being a list object intermediary between "__main__" (the running namespace) and the shell command used to start a script.

For example: 

$ ./get_chem.py Au 

passes Au as a string string element to the running get_chem.py, which finds it in sys.argv[1].  'get_chem.py' itself, the name of the running script, is what's in sys.argv[0].
 
Here's an example lookup operating, running the above lookup about gold ("Au"):

Server and Client 

That's Python's standard library CGI server in the background, plain vanilla.  The going rate is not included. This pared down database lists he abbreviation and longer name, number of protons, and atomic mass.  The long integer and KTU track when a user last touched the record, me in this case.

>>> num
1493462392
>>> datetime.datetime.fromtimestamp(num)
datetime.datetime(2017, 4, 29, 3, 39, 52)


The invocation in the foreground triggers a little script to send an HTTP request to said server, with a chemical element symbol (e.g. Au for Gold), which, if all goes well, returns a JSON string which the little script converts, to a list. Print to console. We're done:

looking up an element through localhost

Taking a look at that script, we see the Python DBI in action. That could have been Oracle we were talking to, like in the good old days (mythological allusion).

Serving Data

I've changed the shebang line to /usr/bin/env python, which is fine if you're running Python3 or greater, and shared the two scripts via Github, for educational use.  Thanks to WorkingIt!

The database table, periodic_table.db, is likewise available.  I'm continuing to evolve this little project, adding AJAX to the mix.