2010-11-20 25 views

答えて

27

私は一緒にチュートリアルに基づいて迅速かつ汚い例をたたきました。私のローカルappengine SDKでテストされています。

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): 

     # obtain ip address 
     ip = self.request.remote_addr 

     # create a new Log record 
     log = Log() 

     # assign ip address to the ip_address field 
     log.ip_address = ip 

     # no need to set access_time because 
     # of the auto_now_add=True setting defined in the Log model 

     # save to the datastore 
     log.put() 

     # output 
     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 + ',') 

application = webapp.WSGIApplication([('/', MainPage), ('/logs', LogPage)], 
            debug=True) 

def main(): 
    run_wsgi_app(application) 

if __name__ == "__main__": 
    main() 
+0

うわー、素晴らしい。ログは正常ですが、500サーバーエラーが発生します。http://iantest123.appspot.com/ –

+0

私の悪い - 私はそれを誤ってコピーしました。よく働く! –

28

で試してみてください:

os.environ["REMOTE_ADDR"] 

またはRequest Class variableと:

class MyRequestHandler(webapp.RequestHandler): 
    def get(self): 
     ip = self.request.remote_addr 
+0

@pyfuncはそれが間違っていないと信じています。 – systempuntoout

+0

@pyfunc downvotingの前に、人々がos.environをどのように使用するかを読んでください。そして2番目の部分は私の答えでした。私はOPによって尋ねられたより広範な質問に答えていました。ただちょっとリラックスしてください。 – systempuntoout

+0

@pyfuncそれは私の答えの一部なので、編集は全くありません。 – systempuntoout