Finding a public facing IP address in Python?

2019-01-23 03:54发布

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

8条回答
萌系小妹纸
2楼-- · 2019-01-23 03:57
import requests
r = requests.get(r'http://jsonip.com')
# r = requests.get(r'https://ifconfig.co/json')
ip= r.json()['ip']
print('Your IP is {}'.format(ip))

Reference

查看更多
做个烂人
3楼-- · 2019-01-23 04:03
import urllib2
text = urllib2.urlopen('http://www.whatismyip.org').read()
urlRE=re.findall('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',text)
urlRE        

['146.148.123.123']

Try putting whatever 'findmyipsite' you can find into a list and iterating through them for comparison. This one seems to work well.

查看更多
倾城 Initia
4楼-- · 2019-01-23 04:04

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.

查看更多
老娘就宠你
5楼-- · 2019-01-23 04:06

https://api.ipify.org/?format=json is pretty straight forward

can be parsed by just running requests.get("https://api.ipify.org/?format=json").json()['ip']

查看更多
Anthone
6楼-- · 2019-01-23 04:12

If you don't mind expletives then try:

http://wtfismyip.com/json

Bind it up in the usual urllib stuff as others have shown.

There's also:

http://www.networksecuritytoolkit.org/nst/tools/ip.php

查看更多
姐就是有狂的资本
7楼-- · 2019-01-23 04:13

All of the answers I see above would report the IP address of any web proxy in use, not necessarily the public facing IP address of your system (anything not being run through a web proxy may have an entirely different IP address).

查看更多
登录 后发表回答