Last active
November 5, 2022 00:05
-
-
Save geekscape/a42bb05377a6fc67bf98eefb763fbd2c to your computer and use it in GitHub Desktop.
Python web server example
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Hello World !</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<body> | |
<script> | |
if (typeof DeviceOrientationEvent.requestPermission === 'function') { | |
DeviceOrientationEvent.requestPermission() | |
.then(permissionState => { | |
if (permissionState === 'granted') { | |
window.addEventListener('deviceorientation', () => {}); | |
} | |
}) | |
.catch(console.error); | |
} else { | |
// handle regular non iOS 13+ devices | |
} | |
</script> | |
<div> | |
<p>This is a test</p> | |
</div> | |
<form method="POST"> | |
<p>Enter Wi-Fi access details:</p> | |
SSID:<input type="text" name="ssid"/><br/> | |
Password:<input type="input" name="password"/><br/> | |
<!--Password:<input type="password" name="password"/><br/> --!> | |
<input type="submit" value="Enter"/><br/> | |
</form> | |
</body> | |
</html> |
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
#!/usr/bin/env python3 | |
# | |
# https://tinyurl.com/gs-py-web-server | |
# https://gist.github.com/geekscape/a42bb05377a6fc67bf98eefb763fbd2c | |
# | |
# pip install flask # Includes Jinja2 (template engine) | |
# mkdir templates | |
# vi templates/index.html | |
# ./web_server.py | |
# | |
# firefox http://127.0.0.1:5000 | |
# firefox http://127.0.0.1:5000/hello/YOUR_NAME | |
# | |
# HTTP methods | |
# ~~~~~~~~~~~~ | |
# GET: URL states which page to return, optional request data in the URL | |
# POST: Like GET, but HTML <form> contains request data | |
# HEAD: Same as GET method, but no response body is returned | |
# PUT: Update target resource with uploaded data | |
# DELETE: Delete target resource given by the URL | |
from flask import Flask, redirect, render_template, request, url_for | |
app = Flask(__name__) | |
@app.route("/", methods = ["POST", "GET"]) # http://host:port | |
def index(): | |
if request.method == "GET": | |
return render_template("index.html") | |
if request.method == "POST": | |
ssid = request.form["ssid"] | |
password = request.form["password"] | |
print(f"User entered SSID: {ssid}, password: {password}") | |
return redirect(url_for("hello_name", name=ssid)) | |
@app.route("/hi") # http://host:port/hi/NAME | |
def hi(): | |
return "Hello World !" | |
@app.route("/hello/<name>") # http://host:port/hello/NAME | |
def hello_name(name): | |
return f"Hello {name} !" | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment