Search code examples
pythonip-address

Finding a public facing IP address in Python?


How can I find the public facing IP for my net work in Python?


Solution

  • This will fetch your remote IP address

    import urllib
    ip = urllib.urlopen('http://automation.whatismyip.com/n09230945.asp').read()
    

    If you don't want to rely on someone else, then just upload something like this PHP script:

    <?php echo $_SERVER['REMOTE_ADDR']; ?>
    

    and change the URL in the Python or if you prefer ASP:

    <%
    Dim UserIPAddress
    UserIPAddress = Request.ServerVariables("REMOTE_ADDR")
    %>
    

    Note: I don't know ASP, but I figured it might be useful to have here so I googled.