Skip to content

Instantly share code, notes, and snippets.

@scturtle
Last active February 19, 2022 09:12
Show Gist options
  • Save scturtle/abb4d6261a9de44b04b8 to your computer and use it in GitHub Desktop.
Save scturtle/abb4d6261a9de44b04b8 to your computer and use it in GitHub Desktop.
Let's go HTTPS!
from gevent import monkey
monkey.patch_all()
import bottle
class SSLWebServer(bottle.ServerAdapter):
def run(self, handler):
from gevent.pywsgi import WSGIServer
srv = WSGIServer((self.host, self.port), handler,
certfile='/etc/letsencrypt/live/XXX/fullchain.pem',
keyfile='/etc/letsencrypt/live/XXX/privkey.pem')
srv.serve_forever()
@bottle.route('/')
def root():
return 'Hello letsencrypt!'
bottle.run(host='XXX', port=443, server=SSLWebServer)
from bottle import ServerAdapter, route, run, server_names
class SSLWebServer(ServerAdapter):
def run(self, handler):
from cherrypy.wsgiserver import CherryPyWSGIServer
from cherrypy.wsgiserver.ssl_builtin import BuiltinSSLAdapter
server = CherryPyWSGIServer((self.host, self.port), handler)
server.ssl_adapter = BuiltinSSLAdapter(
certificate="/etc/letsencrypt/live/XXX/cert.pem",
private_key="/etc/letsencrypt/live/XXX/privkey.pem",
certificate_chain="/etc/letsencrypt/live/XXX/chain.pem")
try:
server.start()
except:
server.stop()
@route('/')
def root():
return 'hello letsencrypt!'
server_names['sslwebserver'] = SSLWebServer
run(host='XXX', port=443, server='sslwebserver')
import bottle
class SSLWebServer(bottle.ServerAdapter):
def run(self, handler):
from wsgiref.simple_server import make_server
import ssl
srv = make_server(self.host, self.port, handler, **self.options)
srv.socket = ssl.wrap_socket(
srv.socket, server_side=True,
certfile='/etc/letsencrypt/live/XXX/fullchain.pem',
keyfile='/etc/letsencrypt/live/XXX/privkey.pem')
srv.serve_forever()
@bottle.route('/')
def root():
return 'hello letsencrypt!'
srv = SSLWebServer(host='XXX', port=443)
bottle.run(server=srv)
import bottle
@bottle.hook('before_request')
def redirect():
r = bottle.request
if r.get_header('X-Forwarded-Proto', 'http') != 'https':
if r.url.startswith('http:'):
bottle.redirect(r.url.replace('http', 'https', 1), 302)
bottle.run(host='XXX', port=80)
@shekhirin
Copy link

Thank you so much!

@chiguranton
Copy link

LIKE

@RevoluPowered
Copy link

You can also do this
bottle.run(host='XXX', port=443, server='gevent', certfile='/etc/letsencrypt/live/XXX/fullchain.pem', keyfile='/etc/letsencrypt/live/XXX/privkey.pem')

@ptv1p3r
Copy link

ptv1p3r commented Feb 1, 2020

What if i need running ssl on a different port say 4000 ? that seems to be my problem, 443 it's in use by one other web server...

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