範例程式:網頁程式中的資訊安全

步驟一:安裝 flask

!pip install flask

步驟二:引入 flask

from flask import Flask, request
from markupsafe import escape

步驟三:路由撰寫

app = Flask(__name__)
# Define Flask routes
@app.route("/")
def index():
    return "Hello World"

@app.route("/hello")
def bighello():
    return "<h1>Hello from Colab!</h1>"

@app.route('/user', methods=['GET'])
def show_user_profile():
    username = request.args.get('username')
    return '<html><body><h1>hello {}</h1></body></html>'.format(username)

@app.route('/user_escape', methods=['GET'])
def show_user_profile_escape():
    username = request.args.get('username')
    return '<html><body><h1>hello {}</h1></body></html>'.format(escape(username))

@app.route('/calc', methods=['GET'])
def my_calc():
  expr = request.args.get('expr')
  return "{}={}".format(expr,eval(expr))

步驟四:啟用 server 及對外連線

import multiprocessing
proc = multiprocessing.Process(target=app.run, kwargs=dict(use_reloader=False))
proc.start()
!ssh  -o "StrictHostKeyChecking no" -R 80:localhost:5000 serveo.net
proc.terminate()