how do i run multiple python apps in 1 command line under 1 wsgi app?
TRANSCRIPT
SUSAN TAN CISCO IN SAN FRANCISCO TWITTER: @ARCTANSUSAN
HOW DO I RUN MULTIPLE PYTHON APPS IN 1 COMMAND LINE UNDER 1 WSGI?
THIS IS A HELLO WORLD FLASK APP
from flask import Flask, request app = Flask(__name__)
@app.route('/<name>') def hello_world(name): return "Hello %s, I'm a flask app!" % (name)
if __name__ == '__main__': app.debug = True app.run()
THIS IS A HELLO WORLD DJANGO APP
from django.http import HttpResponse
def hello(request, name): return HttpResponse("<p>Hello, %s I'm a Django app.</p>" % (name,))
views.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('', url(r'^(?P<name>[-\w]+)/$', 'hello_django.mysite.myapp.views.hello'), )
urls.py
Django app runs at http://127.0.0.1:8000/ Flask app runs at http://127.0.0.1:5000/
An Observation
HOW DO I COMBINE BOTH PYTHON APPS?SO THEY’RE BOTH ON SAME PORT? SAME COMMAND LINE ?
WSGI DISPATCHER
Source: Reference: http://flask.pocoo.org/docs/0.10/patterns/appdispatch/
CONFIGURE WSGI.PY IN DJANGO APP
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_django.mysite.myapp.settings")
# This application object is used by any WSGI server configured to use this # file. This includes Django's development server, if the WSGI_APPLICATION # setting points here.
from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
wsgi.py
THIS IS A HELLO WORLD COMBINED FLASK & DJANGO APP
from werkzeug.serving import run_simple from werkzeug.wsgi import DispatcherMiddleware
import hello_flask from hello_django.mysite.myapp import wsgi as hello_django
application = DispatcherMiddleware(hello_flask.app, { '/django': hello_django.application })
if __name__ == '__main__': run_simple('localhost', 4000, application, use_reloader=True, use_debugger=True, use_evalex=True)
combined_apps.py
Let’s run more python web frameworks in 1 command line
LIVE DEMO TIME
Source Code? https://github.com/ArcTanSusan/wsgi_lightnng_talk
Reference: http://flask.pocoo.org/docs/0.10/patterns/appdispatch/
Slides? On slideshare. I tweeted them already.
Contact Info Susan Tan
Twitter: @ArcTanSusan