|
| 1 | +# Copyright 2016 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 different ways of fetching |
| 17 | +URLS on App Engine |
| 18 | +""" |
| 19 | + |
| 20 | +import logging |
| 21 | +# [START url-imports] |
| 22 | +import urllib |
| 23 | +import urllib2 |
| 24 | + |
| 25 | +from google.appengine.api import urlfetch |
| 26 | +import webapp2 |
| 27 | +# [END url-imports] |
| 28 | + |
| 29 | + |
| 30 | +class UrlLibFetchHandler(webapp2.RequestHandler): |
| 31 | + """ Demonstrates an HTTP query using urllib2""" |
| 32 | + |
| 33 | + def get(self): |
| 34 | + # [START urllib-get] |
| 35 | + url = "http://www.google.com/" |
| 36 | + try: |
| 37 | + result = urllib2.urlopen(url) |
| 38 | + self.response.write(result.read()) |
| 39 | + except urllib2.URLError, e: |
| 40 | + logging.error("Caught exception fetching url {}".format(e)) |
| 41 | + # [END urllib-get] |
| 42 | + |
| 43 | + |
| 44 | +class UrlFetchHandler(webapp2.RequestHandler): |
| 45 | + """ Demonstrates an HTTP query using urlfetch""" |
| 46 | + |
| 47 | + def get(self): |
| 48 | + # [START urlfetch-get] |
| 49 | + url = "http://www.googleadsfasdf.com/" |
| 50 | + try: |
| 51 | + result = urlfetch.fetch(url) |
| 52 | + if result.status_code == 200: |
| 53 | + self.response.write(result.content) |
| 54 | + else: |
| 55 | + self.response.status_code = result.status_code |
| 56 | + except urlfetch.Error, e: |
| 57 | + logging.error("Caught exception fetching url {}".format(e)) |
| 58 | + # [END urlfetch-get] |
| 59 | + |
| 60 | + |
| 61 | +class UrlPostHandler(webapp2.RequestHandler): |
| 62 | + """ Demonstrates an HTTP POST form query using urlfetch""" |
| 63 | + |
| 64 | + form_fields = { |
| 65 | + "first_name": "Albert", |
| 66 | + "last_name": "Johnson", |
| 67 | + } |
| 68 | + |
| 69 | + def get(self): |
| 70 | + # [START urlfetch-post] |
| 71 | + try: |
| 72 | + form_data = urllib.urlencode(UrlPostHandler.form_fields) |
| 73 | + headers = {'Content-Type': 'application/x-www-form-urlencoded'} |
| 74 | + result = urlfetch.fetch( |
| 75 | + url="http://localhost:8080/submit_form", |
| 76 | + payload=form_data, |
| 77 | + method=urlfetch.POST, |
| 78 | + headers=headers) |
| 79 | + self.response.write(result.content) |
| 80 | + except urlfetch.Error, e: |
| 81 | + logging.error("Caught exception fetching url {}".format(e)) |
| 82 | + # [END urlfetch-post] |
| 83 | + |
| 84 | + |
| 85 | +class SubmitHandler(webapp2.RequestHandler): |
| 86 | + """ Handler that receives UrlPostHandler POST request""" |
| 87 | + |
| 88 | + def post(self): |
| 89 | + self.response.out.write((self.request.get('first_name'))) |
| 90 | + |
| 91 | + |
| 92 | +app = webapp2.WSGIApplication([ |
| 93 | + ('/', UrlLibFetchHandler), |
| 94 | + ('/url_fetch', UrlFetchHandler), |
| 95 | + ('/url_post', UrlPostHandler), |
| 96 | + ('/submit_form', SubmitHandler) |
| 97 | +], debug=True) |
0 commit comments