Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get IP address in Google App Engine + Python

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

Thanks!

like image 705
Ian McIntyre Silber Avatar asked Nov 20 '10 03:11

Ian McIntyre Silber


People also ask

How do I find my App Engine IP address?

You can find the current IP address ranges for your App Engine services based on IP range information that Google publishes: Google publishes the complete list of IP ranges that it makes available to users on the internet in goog. json.

What is the IP address of Google search engine?

Google's IP address is 216.58. 217.206. Since that's harder to remember than “google.com,” we typically just memorize the latter. Still, IP addresses are essential to how the internet works, as they allow us to identify and communicate with any device connected to the internet.

How do you check IP address in GCP?

You view the internal and external IP addresses for your instance through either the Google Cloud console, the Google Cloud CLI, or the Compute Engine API. In the Google Cloud console, go to the VM instances page. If the VM instance has an external IP address, it appears under the External IP column.


2 Answers

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()
like image 106
Josh Avatar answered Oct 07 '22 02:10

Josh


Try with:

os.environ["REMOTE_ADDR"]

or with the Request Class variable:

class MyRequestHandler(webapp.RequestHandler):
    def get(self):
        ip = self.request.remote_addr
like image 33
systempuntoout Avatar answered Oct 07 '22 01:10

systempuntoout