Skip to content

Instantly share code, notes, and snippets.

@FZX
Forked from scturtle/gevent.py
Created April 23, 2017 10:43
Show Gist options
  • Save FZX/74bac6f49ec65f5844a03bbd1d685942 to your computer and use it in GitHub Desktop.
Save FZX/74bac6f49ec65f5844a03bbd1d685942 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment