2008-10-03 38 views

回答

14

這將獲取遠程IP地址

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

如果你不想依賴別人,那麼只需上傳這樣的PHP腳本:

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

並更改網址在Python或者如果你喜歡ASP:

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

注:我不知道ASP,但我想它可能是有用的哈在這裏,所以我GOOGLE了。

+1

[php手冊](http://www.php.net/manual/en/reserved.variables.server.php)說`REMOTE_ADDR`是*「用戶正在查看當前頁面的IP地址。」 *即,您需要將php腳本放在外部服務器上以獲取您的公共IP。另外`automation.whatismyip.com`不可訪問。 `http:// canihazip.com/s`的作品。 – jfs 2014-02-24 21:41:26

+0

似乎automation.whatismyip.com已不再運行,他們希望您創建一個帳戶並使用他們的主頁。 – 2015-02-08 05:45:02

8

whatismyip.org更好...它只是拋棄ip作爲明文,沒有多餘的廢話。

import urllib 
ip = urllib.urlopen('http://whatismyip.org').read() 

但是,是的,不可能很容易地做到這一點,而不依賴於網絡之外的東西。

+0

我也沒有引用鏈接,它也只是返回原始IP地址。 – UnkwnTech 2008-10-03 12:31:26

+1

不再只是給出明文。 – powlo 2013-04-30 16:06:09

+0

也似乎認爲我住在堪薩斯州。我們不再在堪薩斯州託託。 – powlo 2013-04-30 16:14:18

1

我看到上面將報告的IP地址的答案的任何Web代理正在使用中,而不一定是您的系統的公共IP地址(任何未通過Web代理運行的IP地址都可能具有完全不同的IP地址)。

2
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'] 

嘗試把什麼 'findmyipsite' 你可以找到成一個列表,並通過它們進行比較迭代。這一個似乎運作良好。

0

這是簡單

>>> import urllib 
>>> urllib.urlopen('http://icanhazip.com/').read().strip('\n') 
'xx.xx.xx.xx' 
0
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