Skip to content

Instantly share code, notes, and snippets.

@hartfordfive
Forked from 5j9/wsgiref_flask.py
Created April 27, 2023 21:31
Show Gist options
  • Save hartfordfive/0f9d96834fc8167e41020be6f73786ef to your computer and use it in GitHub Desktop.
Save hartfordfive/0f9d96834fc8167e41020be6f73786ef 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