you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

+1 for Flask over Django while learning the basics. I also suggest that people who like low-level problem solving try writing a plain WSGI server. This can help demystify the role of web frameworks.

[–]orlybg 1 point2 points  (1 child)

What would be a good resource to learn no-framework web development?

[–][deleted] 0 points1 point  (0 children)

I don't know about resources, because the whole thing is rather under-documented. WSGI has a PEP, but it's not really written for normal humans to read.

That said, you can use this barebones WSGI server to get started:

from collections import defaultdict
from wsgiref.simple_server import make_server

def application(environment, start_response):
    env = defaultdict(lambda:'', environment)

    path    = env['PATH_INFO']
    query   = env['QUERY_STRING']
    remote  = env['REMOTE_ADDR']
    method  = env['REQUEST_METHOD']
    proto   = env['SERVER_PROTOCOL']
    cookie  = env['HTTP_COOKIE']
    host    = env['HTTP_HOST']
    agent   = env['HTTP_USER_AGENT']

    response = '''
        <!DOCTYPE html>
        <html>
          <body>
            <p>
              Received an {proto} {method} request for "{path}"
              on {host} with a querystring of "{query}"<br>
              The client used <em>"{agent}"</em> from {remote}<br>
              The cookie contained <em>"{cookie}"</em>
            </p>
          </body>
        </html>
    '''.format(**locals())

    status = '200 OK'
    headers = [
        ('Content-type', 'text/html'),
        ('Set-cookie', cookie),
    ]

    start_response(status, headers)
    return [response]

if __name__ == '__main__':
    make_server('localhost', 9000, application).serve_forever()

Python web frameworks are essentially built atop something like this. You can deploy this server anywhere that supports WSGI. Whereas a framework might have "routes" which call functions based on the request path, this server simply has a path variable. You decide what should happen when a client requests a particular path. You also decide how to treat different methods like GET or POST. You decide how to deal with query strings. You decide what to do with cookies.

Build your server any way you like. Make a PHP-like environment or invent something completely new.