Created
December 21, 2020 16:13
-
-
Save 5j9/97e671d165d880fc7bdb0ef782a9b809 to your computer and use it in GitHub Desktop.
running flask app using python's built-in wsgiref
This file contains hidden or 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
# call this server using the following js command from the browser console: | |
# (await fetch('http://localhost:5000/', {method: "POST", body: '5'})).text() | |
# note: wsgiref is only good for development and in that case Werkzeug is | |
# usually preferred. See http://mitsuhiko.pocoo.org/wzdoc/wsgihowto.html | |
from wsgiref.simple_server import make_server | |
from flask import Flask, request, Response | |
app = Flask(__name__) | |
# the default method is 'GET' | |
@app.route('/', methods=['GET', 'POST']) | |
def foobar(): | |
print(request.data) | |
# the header is required to get over the CORS policy of the browsers | |
return Response('foo bar', headers=(('Access-Control-Allow-Origin', '*'),)) | |
server = make_server('localhost', 5000, app) | |
# serve_forever runs in a loop. to serve only a single request use: | |
# server.handle_request() | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment