0

I am new to web programming. Now unfortunately, I was a kid during the advent of the world wide web and didn't have access either. So now when I am just starting out, it feels like there's a myriad of concepts to learn and understand.

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.

How do these programming languages and CGI scripts written in C(?) relate?

Are these frameworks somehow converting code written in Ruby or Python into CGI or are they doing something else entirely? It feels like there's too much to know to even understand how the simplest dynamic website works.

Also, do I need to learn HTTP in depth before I even begin learning to build Web apps?

Silver
  • 103
  • 3

1 Answers1

1

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.

Joe Reid
  • 121
  • 3
  • Fantastic! This is exactly what I needed to know. I actually already know how to program in Python and Ruby but no one ever really explains how exactly is it that the requests sent by the browser are handled by your code. People just focus on giving you a set of instructions to do and then expect you to feel great about some output you get without ever understanding any of the underlying mechanism. – Silver Jun 09 '15 at 18:50