27

I'm looking for the equivalent of <?php $_SERVER['REMOTE_ADDR'] ?> in Google App Engine and Python.

Thanks!

Ian McIntyre Silber
  • 5,553
  • 13
  • 53
  • 76

4 Answers4

30

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()
Josh
  • 2,158
  • 18
  • 22
29

Try with:

os.environ["REMOTE_ADDR"]

or with the Request Class variable:

class MyRequestHandler(webapp.RequestHandler):
    def get(self):
        ip = self.request.remote_addr
pyfunc
  • 65,343
  • 15
  • 148
  • 136
systempuntoout
  • 71,966
  • 47
  • 171
  • 241
  • @pyfunc before downvoting, read how people use os.environ. And the second part was in my answer, I was answering a more broad question asked by the OP and then deleted. Just relax a little bit, thanks. – systempuntoout Nov 20 '10 at 03:44
  • 2
    @pyfunc offsound got the accepted mark, that's funny. And I got -1 just for you. That's funny too. – systempuntoout Nov 20 '10 at 04:06
  • @systemppuntoout - I agree your answer probably should have been the accepted answer as it was technically the first and it concisely answers Ian's question. I was actually answering a very similar, but more broad question Ian had posted before this one, and apparently deleted before I could post my answer. Then a bit later I found this new question, so still having the code saved, I posted it here. – Josh Nov 20 '10 at 04:48
  • 5
    +1; OP asked for equivalent of PHP's access to environment; doesn't presuppose using webapp – Wooble Nov 20 '10 at 23:06
  • I use aiohttp on appengine python 3.7 standard env,not work for me.. "File "/opt/python3.7/lib/python3.7/os.py", line 678, in __getitem__ raise KeyError(key) from None KeyError: 'REMOTE_ADDR" –  Aug 17 '19 at 04:32
0
request.headers['X-AppEngine-User-IP']

works for me, I use appengine python3.7 standard env.

  • is explicitly listed as for google internal use, ie. this could change at any time. Read the docs: https://cloud.google.com/appengine/docs/standard/nodejs/reference/request-response-headers – xaphod Sep 22 '21 at 23:50
0

The official answer from Google is here:

https://cloud.google.com/appengine/docs/standard/python3/reference/request-response-headers

xaphod
  • 6,392
  • 2
  • 37
  • 45