python - 在 Google App Engine + Python 中获取 IP 地址

标签 python google-app-engine

我正在寻找 <?php $_SERVER['REMOTE_ADDR'] ?> 的等价物在 Google App Engine 和 Python 中。

谢谢!

最佳答案

我根据教程一起拍了一个快速而肮脏的例子。它已经在我的本地 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()

关于python - 在 Google App Engine + Python 中获取 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4231077/

相关文章:

python - numpy arctan2 参数根据语法导致 ValueError

python - CentOS 上的 Setup.py : install lxml with Python2. 6

python - 有没有更简洁的方法来获取某些内容的第一次出现?

python - 发现 CSV 映射强制到 Unicode : need string or buffer, 列表

file - 从 AppEngine 部署中删除不需要的上传

java - 在哪里可以找到 DatastoreViewerServlet - GAE?

python - Google App Engine Flask 413 请求实体太大错误

javascript - Node.js 中的 Google datastore.lookup 没有返回结果

python - 剥离 Html 标签 Findall + Beautiful Soup

java - JavaScript中区分内网和外网IP地址