For example, I just read about CGI scripts. Now, usually when I see people talk about web application programming these days, they're using some framework for Ruby or Python or even PHP.
CGI is considered by most to be an outdated technology these days for anything but the simplest applications, some of your reference material may be outdated.
How do these programming languages and CGI scripts written in C(?) relate?
OK, let's say that we had some perl code as a CGI script:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, world!\n";
When there is an HTTP Request sent to the server for this file (eg: http://www.someserver.com/cgi-bin/hello.pl
) the server will first execute this code and send the standard output as a response. This is the reason that we have to include the "Content-type: text/html\n\n"
in the response as the HTTP header.
Web Frameworks however are a rather different beast. In their most basic description they are simply a blanket term for any tool-set that provides functionality associated with common web development activities.
In practice however they almost all function in a similar way and provide similar tooling.
From Wikipedia:
many frameworks provide libraries for database access, templating frameworks and session management
Let's compare the previous CGI code to this python code written using a Web Framework called Flask:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
To avoid as much of the Python specific syntax as possible let us reduce this code to:
import Flask
app = Flask()
@app.route('/')
def hello_world():
return 'Hello World!'
app.run()
It is important to remember that this code will not actually run it's only shortened for clarity.
The first line imports our application framework.
The next defines a variable app that will reference our application
The def hello_world():
block defines a function that returns the string Hello World!
The @app.route('/')
says to run this function when the URL route /
is requested. Or, for clarity, when you point your browser at http://www.someserver.com/
app.run()
then starts a new web server on the machine that serves the routes of our application.
These two pieces of code (CGI perl and Flask python) functionally do the same thing but achieve it in a very different way. You can see for example in the Flask application we didn't need the "Content-type: text/html\n\n"
line. This is because the framework takes care of these things for us in the internal web server.
I would recommend following a good tutorial for a python web framework like Flask as a good starting point for understanding what a web framework is and how it works. For example, this one: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
Also, do I need to learn HTTP in depth before I even begin learning to build Web apps?
Not in great depth, a basic understanding of GET/POST and status codes will be all you need to start learning most web frameworks. That said knowledge is always good, you can fill in your understanding as you progress but don't focus entirely on completely understanding the inter-workings of every sub-component as that in our field is quite frankly an unobtainable goal.
I hope this helped.