how do i run multiple python apps in 1 command line under 1 wsgi app?

11

Click here to load reader

Upload: susan-tan

Post on 17-Jan-2017

464 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: How do I run multiple python apps in 1 command line under 1 WSGI app?

SUSAN TAN CISCO IN SAN FRANCISCO TWITTER: @ARCTANSUSAN

HOW DO I RUN MULTIPLE PYTHON APPS IN 1 COMMAND LINE UNDER 1 WSGI?

Page 2: How do I run multiple python apps in 1 command line under 1 WSGI app?

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()

Page 3: How do I run multiple python apps in 1 command line under 1 WSGI app?

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

Page 4: How do I run multiple python apps in 1 command line under 1 WSGI app?

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 ?

Page 5: How do I run multiple python apps in 1 command line under 1 WSGI app?

WSGI DISPATCHER

Page 6: How do I run multiple python apps in 1 command line under 1 WSGI app?

Source: Reference: http://flask.pocoo.org/docs/0.10/patterns/appdispatch/

Page 7: How do I run multiple python apps in 1 command line under 1 WSGI app?

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

Page 8: How do I run multiple python apps in 1 command line under 1 WSGI app?

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

Page 9: How do I run multiple python apps in 1 command line under 1 WSGI app?
Page 10: How do I run multiple python apps in 1 command line under 1 WSGI app?

Let’s run more python web frameworks in 1 command line

LIVE DEMO TIME

Page 11: How do I run multiple python apps in 1 command line under 1 WSGI app?

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