Last active
February 19, 2022 09:12
-
-
Save scturtle/abb4d6261a9de44b04b8 to your computer and use it in GitHub Desktop.
Let's go HTTPS!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
LIKE
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')
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
Thank you so much!