|
| 1 | +# Copyright 2015 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Sample application that demonstrates various aspects of App Engine's request |
| 17 | +handling. |
| 18 | +""" |
| 19 | + |
| 20 | +import os |
| 21 | +import time |
| 22 | + |
| 23 | +import webapp2 |
| 24 | + |
| 25 | + |
| 26 | +# [START request_timer] |
| 27 | +class TimerHandler(webapp2.RequestHandler): |
| 28 | + def get(self): |
| 29 | + from google.appengine.runtime import DeadlineExceededError |
| 30 | + |
| 31 | + try: |
| 32 | + time.sleep(70) |
| 33 | + self.response.write('Completed.') |
| 34 | + except DeadlineExceededError: |
| 35 | + self.response.clear() |
| 36 | + self.response.set_status(500) |
| 37 | + self.response.out.write( |
| 38 | + 'The request did not complete in time.') |
| 39 | +# [END request_timer] |
| 40 | + |
| 41 | + |
| 42 | +# [START environment] |
| 43 | +class PrintEnvironmentHandler(webapp2.RequestHandler): |
| 44 | + def get(self): |
| 45 | + self.response.headers['Content-Type'] = 'text/plain' |
| 46 | + for key, value in os.environ.iteritems(): |
| 47 | + self.response.out.write( |
| 48 | + "{} = {}\n".format(key, value)) |
| 49 | +# [END environment] |
| 50 | + |
| 51 | + |
| 52 | +# [START request_ids] |
| 53 | +class RequestIdHandler(webapp2.RequestHandler): |
| 54 | + def get(self): |
| 55 | + self.response.headers['Content-Type'] = 'text/plain' |
| 56 | + request_id = os.environ.get('REQUEST_LOG_ID') |
| 57 | + self.response.write( |
| 58 | + 'REQUEST_LOG_ID={}'.format(request_id)) |
| 59 | +# [END request_ids] |
| 60 | + |
| 61 | + |
| 62 | +app = webapp2.WSGIApplication([ |
| 63 | + ('/timer', TimerHandler), |
| 64 | + ('/environment', PrintEnvironmentHandler), |
| 65 | + ('/requestid', RequestIdHandler) |
| 66 | +], debug=True) |
0 commit comments