Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make bottle server HTTPS python

I'm using Bottle as my webservice. Currently, its running on bottle's default wsgi server and handles HTTP requests. I want to encrypt my webservice and handle HTTPS requests. Can someone suggest a method for this. I tried running on cherrypy server, but the latest version is not supporting the pyOpenSSLAdapter.

like image 335
Gaurav Ram Avatar asked May 16 '17 23:05

Gaurav Ram


3 Answers

As you know bottle also supports Gunicorn. You can find SSL information at

  • https://docs.gunicorn.org/en/latest/settings.html?highlight=ssl#ssl

Code example

import bottle
from bottle import Bottle

BASE = Bottle()


@BASE.route('/', ['GET'])
def index():
    return 'Index'


bottle.run(
    app=BASE, 
    host='0.0.0.0',
    port='8888',
    server='gunicorn',
    reloader=1,
    debug=1,
    keyfile='key.pem',
    certfile='cert.pem'
)
like image 67
Mo. Atairu Avatar answered Nov 13 '22 20:11

Mo. Atairu


Quick way of achieving https through nginx reverse proxy:-

apt install nginx

Edit /etc/nginx/sites-enabled/default:-

server {
  listen 80 default_server; #listen on port 80
  listen [::]:80 default_server ipv6only=on;

  server_name yourdomain.com www.yourdomain.com; #edit 'yourdomain' with your domain name
  root /var/www/html/; #edit to match wherever your bottle-py root folder is

  location / {
    proxy_pass http://127.0.0.1:8080/; 
    #assuming configuration of bottle-py run() command is 127.0.0.1:8080
  }
}

HTTPS with certbot:-

Login to your domain name provider for 'yourdomain.com' and point 'A-records' to point to your server IP.

apt install certbot python-certbot-nginx
sudo certbot --nginx

Follow the on terminal instructions for certbot. Now bottle-py is served with https by a nginx reverse proxy.

Check https://yourdomain.com and confirm https valid certificate installation.

This is a quick way of doing it. Read more at nginx and certbot documentation.

like image 40
Anoop GR Avatar answered Nov 13 '22 22:11

Anoop GR


You need to put your WSGI server (not WsgiRef certainly) behind a reverse-proxy with https support. Nginx is the most common choice.

like image 39
Roman Miroshnychenko Avatar answered Nov 13 '22 21:11

Roman Miroshnychenko