Flask and lab setup

In this chapter, we will use virtualenv to isolate the environment we will work in. As the name indicates, virtualenv is a tool that creates a virtual environment. It can keep the dependencies required by different projects in separate places while keeping the global site-packages clean. In other words, when you install Flask in the virtual environment, it is only installed in the local virtualenv project directory, not the global site-packages. This make porting the code to other places very easy. 

The chances are high that you may have already come across virtualenv while working with Python before, so we will run through this process quickly. If you have not, feel free to pick up one of many excellent tutorials online, such as http://docs.python-guide.org/en/latest/dev/virtualenvs/.

To use , we will first need to install virtualenv:

# Python 3
$ sudo apt-get install python3-venv
$ python3 -m venv venv

# Python 2
$ sudo apt-get install python-virtualenv
$ virtualenv venv-python2

The proceeding command uses the venv module (-m venv) to get a venv folder with a full Python interpreter inside it. We can use source venv/bin/activate and deactivate to move in and out of the local Python environment:

$ source venv/bin/activate
(venv) $ python
$ which python
/home/echou/Master_Python_Networking_second_edition/Chapter09/venv/bin/python
$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

>>> exit()
(venv) $ deactivate

In this chapter, we will install quite a few Python packages. To make life easier, I have included a requirements.txt file on the book's GitHub repository; we can use it to install all the necessary packages (remember to activate your virtualenv). You should see packages being downloaded and successfully installed at the end of the process: 

(venv) $ pip install -r requirements.txt
Collecting Flask==0.10.1 (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/db/9c/149ba60c47d107f85fe52564133348458f093dd5e6b57a5b60ab9ac517bb/Flask-0.10.1.tar.gz (544kB)
100% |████████████████████████████████| 552kB 2.0MB/s
Collecting Flask-HTTPAuth==2.2.1 (from -r requirements.txt (line 2))
Downloading https://files.pythonhosted.org/packages/13/f3/efc053c66a7231a5a38078a813aee06cd63ca90ab1b3e269b63edd5ff1b2/Flask-HTTPAuth-2.2.1.tar.gz
... <skip>
Running setup.py install for Pygments ... done
Running setup.py install for python-dateutil ... done
Successfully installed Flask-0.10.1 Flask-HTTPAuth-2.2.1 Flask-SQLAlchemy-1.0 Jinja2-2.7.3 MarkupSafe-0.23 Pygments-1.6 SQLAlchemy-0.9.6 Werkzeug-0.9.6 httpie-0.8.0 itsdangerous-0.24 python-dateutil-2.2 requests-2.3.0 six-1.11.0

For our network topology, we will use a simple four-node network, as shown here:

 Lab topology

Let's take a look at Flask in the next section.

Please note that, from here on out, I will assume that you will always execute from the virtual environment and that you have installed the necessary packages in the requirements.txt file.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.250.88