27

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

Thanks!

4 Answers 4

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() 
Sign up to request clarification or add additional context in comments.

2 Comments

wow, awesome. logs fine but gives a 500 Server error iantest123.appspot.com
my bad - I mis-copied it. Works great!
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 

5 Comments

@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.
@pyfunc offsound got the accepted mark, that's funny. And I got -1 just for you. That's funny too.
@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.
+1; OP asked for equivalent of PHP's access to environment; doesn't presuppose using webapp
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"
0
request.headers['X-AppEngine-User-IP'] 

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

1 Comment

is explicitly listed as for google internal use, ie. this could change at any time. Read the docs: cloud.google.com/appengine/docs/standard/nodejs/reference/…
0

The official answer from Google is here:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.