Dynamic IP Notification Using Google App Engine This script is very basic: it does one thing. Connect to it's root and it will log your IP if it has never seen it before. Connect to /logs, and it will show a list of unique IPs within the server storage. I can't take 100 percent credit for it. I grabbed it off of Stack Overflow, modified it to actually run on App Engine, and changed the save mechanism to prevent duplicate entries. This thing is so simple it's shocking; considering how many different super-elaborate ways I can waste money and time doing the same thing. I use it to keep track of my ssh IP. I run wget on my App Engine address every ten minutes. Can this be improved? Are there any flaws which may compromise its efficiency?

helloworld.py

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Log(db.Model):
    access_time = db.DateTimeProperty(auto_now_add=True)
    ip_address = db.StringProperty()
class MainPage(webapp.RequestHandler):
    def get(self):
        ip = self.request.remote_addr
        log = Log()
        logs = Log.all()
        is_new = True
        for log in logs:
                if ip == log: is_new = False
        if is_new:
                log.ip_address = ip
                log.put()
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Logged your visit from ip address %s' % ip)
class LogPage(webapp.RequestHandler):
    def get(self):
        logs = Log.all()

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Ip addresses: ')
        for log in logs:
            self.response.out.write(log.ip_address + ',')
app = webapp.WSGIApplication([('/', MainPage), ('/logs', LogPage)],
                                     debug=True)


app.yaml

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: helloworld.app


P.S. I'm pretty sure this isn't violating any contracts or agreements. Tell me if I'm wrong about that.

Credit: Get IP address in Google App Engine + Python
Lint

PEP 8 recommends two blank lines around class definitions.  Indentation should be consistently 4 spaces — an important convention in Python, where indentation matters a lot.

log = Log() is pointless.

Database

A more Pythonic way to write this…


logs = Log.all()
is_new = True
for log in logs:
        if ip == log: is_new = False



… would be is_new = ip not in Log.all().

But your code wouldn't even work, since an IP address would never be equal to a db.Model object!

It also doesn't scale well, since it doesn't take advantage of the datastore's capabilities.  You should be using a query like this:

Logs.all().filter('ip_address =', ip).run(limit=1)