Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #### Script for EdX course UT6.02X
  2. ## Author: Ramesh Yerraballi
  3. ## Date: Spring 2015
  4. ## License: Freeware
  5. ## The code here is based on three sources:
  6. ## 1. The basic guestbook appengine application from Google
  7. ## 2. pymaps.py script from: https://code.google.com/p/pymaps/
  8. ## 3. ipinfo.io's ip locator is more accurate than geobytes http://ipinfo.io/
  9. ## 4. geobytes.com's ip locator at http://www.geobytes.com/iplocator/
  10. ## and their json api
  11. ## Need the next line to deal with strings that the geobytes server returns that
  12. ## have unicode characters -- Did not Work though
  13. #-*- coding: UTF-8 -*-
  14. import cgi
  15. import time
  16. import logging
  17. import urllib2
  18. import unicodedata
  19. import webapp2
  20. import random
  21. import json
  22. from google.appengine.ext import ndb
  23.  
  24. DEFAULT_LOGBOOK_NAME = 'logbook'
  25.  
  26. # We set a parent key on the 'Greetings' to ensure that they are all
  27. # in the same entity group. Queries across the single entity group
  28. # will be consistent. However, the write rate should be limited to
  29. # ~1/second.
  30.  
  31. def logbook_key(logbook_name=DEFAULT_LOGBOOK_NAME):
  32. """Constructs a Datastore key for a logbook entity.
  33.  
  34. We use logbook_name as the key.
  35. """
  36. return ndb.Key('logbook', logbook_name)
  37.  
  38.  
  39. # [START greeting]
  40. class Greeting(ndb.Model):
  41. """A main model for representing an individual Guestbook entry."""
  42. author = ndb.StringProperty(indexed=False)
  43. greet = ndb.StringProperty(indexed=False)
  44. date = ndb.DateTimeProperty(auto_now_add=True)
  45. city = ndb.StringProperty(indexed=False)
  46. ipaddr = ndb.StringProperty(indexed=False)
  47. # [END greeting]
  48. MAIN_PAGE_HTML = """\
  49. <html>
  50. <body>
  51. <form action="/sign" method="post">
  52. <div><input type="text" name="uid" cols="4">
  53. <div><input type="text" name="greet" cols="30">
  54. </form>
  55. </body>
  56. </html>
  57. """
  58.  
  59.  
  60. class MainPage(webapp2.RequestHandler):
  61. def get(self):
  62. self.response.write('</body></html>')
  63. self.response.write('<h2>Darren and Alex\'s 445L Lab 4 Logging Server</h2>')
  64. self.response.write('<hr>')
  65. # [START query]
  66. greetings_query = Greeting.query(
  67. ancestor=logbook_key('logbook')).order(-Greeting.date)
  68. greetings = greetings_query.fetch()
  69. # [END query]
  70.  
  71. for greeting in greetings:
  72. self.response.write('<b>%s@%s</b>: <i>%s</i> (accessed from %s)' %
  73. (greeting.author, greeting.city, greeting.greet,
  74. greeting.ipaddr))
  75. self.response.write('<hr>')
  76.  
  77. class Auto(webapp2.RequestHandler):
  78. def get(self):
  79. #comment these three lines for debugging; So we can access it through a browser
  80. if self.request.environ.get('HTTP_USER_AGENT') != 'Keil':
  81. self.response.write('<html><body> <pre> Invalid Access</pre> </body></html>')
  82. return
  83. greeting = Greeting(parent=logbook_key('logbook'))
  84. greeting.ipaddr = self.request.environ.get('REMOTE_ADDR')
  85. geo_url = "http://ipinfo.io/%s/loc" % greeting.ipaddr
  86. # There should be some error checking here if the lookup fails
  87. json_stream = urllib2.urlopen(geo_url)
  88. retstr = json_stream.read()
  89. greeting.author = cgi.escape(self.request.get('id'))
  90. greeting.city = cgi.escape(self.request.get('city'))
  91. greeting.greet = cgi.escape(self.request.get('greet'))
  92. greeting.put()
  93. self.response.write('<html><body>')
  94. self.response.write('<pre>{"%s":"%s", "%s":"%s"}</pre>' %
  95. ("id", greeting.author, "greet", greeting.greet))
  96. self.response.write('</body></html>')
  97.  
  98. application = webapp2.WSGIApplication([
  99. ('/', MainPage),
  100. ('/query', Auto)
  101. ], debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement