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'] ...

Get Python for Google App Engine now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.