Skip to content

Instantly share code, notes, and snippets.

@5j9
Created December 21, 2020 16:13
Show Gist options
  • Save 5j9/97e671d165d880fc7bdb0ef782a9b809 to your computer and use it in GitHub Desktop.
Save 5j9/97e671d165d880fc7bdb0ef782a9b809 to your computer and use it in GitHub Desktop.
running flask app using python's built-in wsgiref
# 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