From b480912887a3952d7863b0189d35b94409435634 Mon Sep 17 00:00:00 2001 From: EagleYing Date: Tue, 22 Jan 2019 10:16:15 +0800 Subject: [PATCH] wechat miniprogram\python\bottle\ssl --- Bottle With Ssl -- https.md | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Bottle With Ssl -- https.md diff --git a/Bottle With Ssl -- https.md b/Bottle With Ssl -- https.md new file mode 100644 index 0000000..04f63be --- /dev/null +++ b/Bottle With Ssl -- https.md @@ -0,0 +1,55 @@ +# Bottle With Ssl -- https + +###Basic + +( You have to download your own ssl certificate for your domain first. I got my ssl.pem free through aliyun . ) + +Bottle is a fast, simple and lightweight [WSGI](http://www.wsgi.org/) micro web-framework for [Python](http://python.org/). We use it in local debugging very simply + +```python +from bottle import Bottle, run + +app = Bottle() + +@app.route('/hello') +def printhello(): + return "Hello World!" + +run(host = '0.0.0.0', port = 8080) +``` + +Then type `python test.py` on your terminal as usual and you can see `Hello world` on your local web page. + +###Operates + +WeChat web program needs `https` instead of `http`, but general [bottle of python](http://bottlepy.org/docs/dev/) only support `http` local degug, we need to add some codes. I found following codes [Bottle with ssl](www.socouldanyone.com/2014/01/bottle-with-ssl.html) and it works well + +```python +from bottle import Bottle, get, run, ServerAdapter + +# copied from bottle. Only changes are to import ssl and wrap the socket +class SSLWSGIRefServer(ServerAdapter): + def run(self, handler): + from wsgiref.simple_server import make_server, WSGIRequestHandler + import ssl + if self.quiet: + class QuietHandler(WSGIRequestHandler): + def log_request(*args, **kw): pass + self.options['handler_class'] = QuietHandler + srv = make_server(self.host, self.port, handler, **self.options) + srv.socket = ssl.wrap_socket ( + srv.socket, + certfile='ssl.pem', # path to certificate + server_side=True) + srv.serve_forever() + +``` + +The you need to add + +```python +srv = SSLWSGIRefServer(host="0.0.0.0", port=8080) +run(server=srv) +``` + +Run your .py file and type `https://yourdomain/hello...` and you will be pleasantly surprised to find a little lock before url. \ No newline at end of file