Book Image

Python for Google App Engine

By : Massimiliano Pippi
Book Image

Python for Google App Engine

By: Massimiliano Pippi

Overview of this book

Table of Contents (15 chapters)
Python for Google App Engine
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Implementing the server


We will start by adding the Python code needed to handle channels on the server side. We expect the JavaScript client will make an HTTP GET request to request a channel, so we add a request handler that will create a channel and return a token in the JSON format to access it. We first import the modules needed at the top of our main.py module:

from google.appengine.api import channel
from utils import get_notification_client_id
import json

Then, we add the code for the request handler:

class GetTokenHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user is None:
            self.abort(401)

        client_id = get_notification_client_id(user)
        token = channel.create_channel(client_id, 60)

        self.response.headers['Content-Type'] = 'application/json'
        self.response.write(json.dumps({'token': token}))

We first check that the user is logged in and return an HTTP 401: Unauthorized error page if this...