Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a718145fe0
Fetching contributors…

Cannot retrieve contributors at this time

116 lines (86 sloc) 2.814 kb
import time
import flask
from flask import Flask, make_response, request
app = Flask(__name__, static_url_path='/examples', static_folder='build')
def get_weather_data(zip):
This is a stubbed function that returns the same static
data each time. In a real server environment, this would
query the database with the given zip parameter.
return {
'location': 'San Francisco',
'image': '/examples/common/partly_cloudy.png',
'temp': 78,
'desc': 'Partly Cloudy'
def weather_widget():
This is the server-side implementation of the widget
from Chapter 1
zip = request.args.get('zip')
data = get_weather_data(zip)
out = '''
'<div>' +
' <p>%s</p>' +
' <img src="%s"/> ' +
' <p><strong>%s &deg;F</strong> &mdash; %s</p>' +
''' % (data['location'], data['image'], data['temp'], data['desc'])
response = make_response(out)
response.headers['Content-Type'] = 'application/javascript'
return response
def slow():
Wait 3 seconds before redirecting to the requested resource
return flask.redirect(request.args.get('url'))
def jsonp():
Returns the passed query string args via JSONP.
return "%s(%s)" % (request.args.get('callback'), json.dumps(request.args))
def cors():
r = make_response(json.dumps(request.args))
# Must match requesting Origin or *
r.headers['Access-Control-Allow-Origin'] = '*'
return r
def trusted_domains(domain):
return ['']
def api_request(request, endpoint):
return make_response(json.dumps({'name': 'Mikon 9000'}))
from urlparse import urlparse
import json
def api(endpoint=None):
domain = urlparse(request.headers['Referer']).hostname
if domain == '':
domain = urlparse(request.headers['X-Publisher-Referer']).hostname
api_key = request.args.get('apiKey')
if domain in trusted_domains(api_key):
return api_request(request, endpoint)
return make_response('Unauthorized domain: %s' % domain, 403)
@app.route('/login', methods=['POST'])
def login():
Sets a cookie
# return flask.redirect('/examples/06/auth-new-window/success.html')
r = make_response(flask.redirect('/examples/06/auth-new-window/success.html'))
r.set_cookie('session_id', 'abcdef123456')
return r
if __name__ == '__main__':
app.debug = True
Jump to Line
Something went wrong with that request. Please try again.