Search code examples
pythonweb-servicesflaskwebserverweb-frameworks

What happen in the background on a web server?


I'm just started to learn back-end web development using Python and Flask framework.

My first application is the simplest one and it returns "Hello World!" when the user send a request for website's homepage.

Below, you can see the structure of my application :

myWebsiteDirectory/
    app/
        __init__.py
    setup.py
    wsgi.py

And below you see the content of the python files:

setup.py

from setuptools import setup

setup(name='YourAppName',
      version='1.0',
      description='OpenShift App',
      author='Your Name',
      author_email='example@example.com',
      url='http://www.python.org/sigs/distutils-sig/',
      install_requires=['Flask>=0.10.1'],
     )

wsgi.py

#!/usr/bin/python
import os

#virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
virtenv = os.path.join(os.environ.get('OPENSHIFT_PYTHON_DIR','.'), 'virtenv')
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
    execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
    pass
#
# IMPORTANT: Put any additional includes below this line.  If placed above this
# line, it's possible required libraries won't be in your searchable path
#

from app import app as application

#
# Below for testing only
#
if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    httpd = make_server('localhost', 8051, application)
    # Wait for a single request, serve it and quit.
    httpd.serve_forever()

__init__.py

from flask import Flask

app = Flask(__name__)
app.debug = True

@app.route('/')
def not_again():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

What is my question:

What happens when I upload this files on the server and what happens when a user request my website?

In the other words:

  1. When Python interpret Each one of the above files on the server? (And how many times each file interpret)?
  2. What happens when a user send a request? His/Her request make a file re-interpreted or the request refers to a running function as an argument? If so, shouldn't there is an infinite loop on the server to catch the request? if so, where is that infinite loop?
  3. What happens when a user send a request when the web server is not finished the previous request yet? Those the argument/script refers/interpret again in a separate environment for this new user or he/she must wait for server to finish answering the previous request?

And again, in the other words:

  • How user's requests handle on a web server?

Although the above question is based on Python & Flask web framework web developing, but it there is a general mechanism for all the languages and frameworks, please let me know that general procedure and not this specific case.


Solution

  • If you have no good idea about how a web server works, since you are interested in Python, I suggest you have a read of:

    If interested then in a walk through of doing something with a Python web framework to build a site, then also consider reading:

    It is a good basic introduction to get people going.

    These will give you fundamentals to work with. How specific WSGI servers or service providers work can then be a bit different, but you will be better able to understand by working through the above first.