Skip to content

Instantly share code, notes, and snippets.

@PankajWorks
Last active July 9, 2018 07:06
Show Gist options
  • Save PankajWorks/a7bb5d776c650a730f0c57e7e4f9a457 to your computer and use it in GitHub Desktop.
Save PankajWorks/a7bb5d776c650a730f0c57e7e4f9a457 to your computer and use it in GitHub Desktop.
Python web frameworks

OS - Ubuntu 16.04.4 LTS

Install

sudo apt-get update
sudo ntpdate pool.ntp.org
sudo apt-get install ntp

Install Python3.7

Install from source

wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
./configure
make 
make install

If you get the following error message zipimport.ZipImportError: can't decompress data; zlib not available

update alternatives
update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.7 0
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1

update-alternatives --config python3
python3 -V

Install Python3.6

sudo apt-get install build-essential checkinstall
sudo apt-get install libreadline-gplv2-dev  libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
cd /usr/src
sudo wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
sudo tar xzf Python-3.6.4.tgz
cd Python-3.6.4
sudo ./configure --enable-optimizations
sudo make altinstall

python3.6 -V
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.6 0
sudo update-alternatives --config python3

Installing from PPA

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt update
sudo apt install python3.6

sudo apt-get install python-virtualenv
sudo apt-get install python-pip
sudo apt-get install python-dev
sudo pip install Flask
sudo pip install gunicorn

cd ~
mkdir flask_app
cd flask_app
vim hello_world_app.py

Add the following

from flask import Flask
app = Flask(__name__)
 
@app.route("/")
def hello():
    return "Hello World!"
 
if __name__ == "__main__":
    app.run()

vim gunicorn.sh

Add the following

NAME="hello_world_app"
FLASKDIR=~/flask_app
SOCKFILE=~/flask_app/sock
USER=root
GROUP=root
NUM_WORKERS=3
 
echo "Starting $NAME"
 
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
 
# Start your gunicorn
exec gunicorn hello_world_app:app -b 0.0.0.0:5000 \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --bind=unix:$SOCKFILE

chmod 755 ~/flask_app/gunicorn.sh

Gunicorn upstart file

cd /etc/init/
sudo vim gunicorn.conf

#Upstart Script
 
description "gunicorn"
 
respawn
respawn limit 15 5
 
start on runlevel [2345]
stop on runlevel [06]
 
env APP_DIR=~/hello_world_app
 
script
chdir $APP_DIR
exec ./gunicorn.sh
end script

nginx configuration

cd /etc/nginx/conf.d/
mkdir /home/ubuntu/www
sudo vim flask.conf

upstream gunicorn_server {
  server localhost:5000 fail_timeout=0;
}
server {
  listen       80;
  server_name  test-server.com;
	root /home/ubuntu/www;
	client_max_body_size 4G;
	keepalive_timeout 5;
  proxy_read_timeout 900;
 
  location / {
	  try_files $uri @app;
  }
location @app {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Real-IP	$remote_addr;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  # pass to the upstream gunicorn server mentioned above 
	proxy_pass http://gunicorn_server;
}
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
   }
sudo service gunicorn start
sudo service nginx restart

Dockerized

(http://tech.osteel.me/posts/2015/01/25/how-to-use-vagrant-for-local-web-development.html)

*Taken from : https://github.com/Bogdanp/molten
*Check : sudo apt-get install python3-venv
*Check : sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 0
*Check : python3 -V # its should be Python 3.6.0 +
*check : export LC_ALL=C

virtualenv -p /usr/bin/python3.6 venv
source venv/bin/activate

pip install molten
pip install gunicorn

Look at documentation present @https://moltenframework.com/guide.html#hello-world

Install

  • virtualenv ~/python_apps/myenv -p /usr/bin/python3
pip install japronto
vim japronto_app.py

Hello World

from japronto import Application


def hello(request):
    return request.Response(text='Hello world!')


app = Application()
app.router.add_route('/', hello)
app.run(debug=True)
python japronto_app.py

Request failed with error

Accepting connections on http://0.0.0.0:8080
Exception in callback UVTransport._call_connection_made
handle: <Handle UVTransport._call_connection_made>
Traceback (most recent call last):
  File "uvloop/cbhandles.pyx", line 67, in uvloop.loop.Handle._run
  File "uvloop/handles/tcp.pyx", line 151, in uvloop.loop.TCPTransport._call_connection_made
  File "uvloop/handles/basetransport.pyx", line 153, in uvloop.loop.UVBaseTransport._call_connection_made
  File "uvloop/handles/basetransport.pyx", line 150, in uvloop.loop.UVBaseTransport._call_connection_made
SystemError: NULL result without error in PyObject_Call
Exception in callback UVTransport._call_connection_made
handle: <Handle UVTransport._call_connection_made>
Traceback (most recent call last):
  File "uvloop/cbhandles.pyx", line 67, in uvloop.loop.Handle._run
  File "uvloop/handles/tcp.pyx", line 151, in uvloop.loop.TCPTransport._call_connection_made
  File "uvloop/handles/basetransport.pyx", line 153, in uvloop.loop.UVBaseTransport._call_connection_made
  File "uvloop/handles/basetransport.pyx", line 150, in uvloop.loop.UVBaseTransport._call_connection_made
SystemError: NULL result without error in PyObject_Call

Install

#sudo apt-get install build-essential python-dev libxml2-dev libxslt1-dev zlib1g-dev libpq-dev
pip install sanic

Sample HelloWorld

from sanic import Sanic
from sanic.response import json

app = Sanic()

@app.route('/')
async def test(request):
    return json({'hello': 'world'})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Dockerized

Look at - https://github.com/itielshwartz/sanic-nginx-docker-example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment