2010-11-20 15 views

उत्तर

27

मैं ट्यूटोरियल के आधार पर एक साथ एक त्वरित और गंदा उदाहरण थप्पड़ मारा। यह मेरे स्थानीय एपेंगेन एसडीके पर परीक्षण किया गया है। आपको इसे अपनी आवश्यकताओं के अनुसार अनुकूलित करने में सक्षम होना चाहिए:

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

कोशिश

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

@pyfunc मानना ​​है या नहीं, यह गलत नहीं है। डाउनवॉटिंग से पहले – systempuntoout

+0

@pyfunc, पढ़ें कि लोग os.en वातावरण का उपयोग कैसे करते हैं। और दूसरा भाग मेरे जवाब में था, मैं ओपी द्वारा पूछे गए एक और व्यापक सवाल का जवाब दे रहा था और फिर हटा दिया गया। बस थोड़ी देर आराम करो, धन्यवाद। – systempuntoout

+0

@pyfunc यह मेरे उत्तर का हिस्सा था, इसलिए कोई संपादन नहीं। – systempuntoout

संबंधित मुद्दे