在Google App Engine + Python中获取IP地址 [英] Get IP address in Google App Engine + Python
本文介绍了在Google App Engine + Python中获取IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Google App Engine和Python中寻找相当于<?php $ _SERVER ['REMOTE_ADDR']?>
。
I'm looking for the equivalent of <?php $_SERVER['REMOTE_ADDR'] ?>
in Google App Engine and Python.
谢谢!
Thanks!
推荐答案
我根据教程打了一个快速而肮脏的例子。它已经在我本地的appengine sdk上测试过了。您应该能够适应您的需求:
I slapped a quick and dirty example together based on the tutorial. It's been tested on my local appengine sdk. You should be able to adapt it to your needs:
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()
这篇关于在Google App Engine + Python中获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文