Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中获取套接字的外部IP?_Python_Sockets - Fatal编程技术网

如何在Python中获取套接字的外部IP?

如何在Python中获取套接字的外部IP?,python,sockets,Python,Sockets,当我在套接字对象上调用socket.getsockname()时,它返回我的机器的内部IP和端口的元组。但是,我想检索我的外部IP。最便宜、最有效的方法是什么?如果没有外部服务器的合作,这是不可能的,因为您和其他计算机之间可能存在任意数量的NAT。如果是自定义协议,您可以要求另一个系统报告它连接到的地址。我能想到的唯一保证提供给您的方法是点击一个服务来获取它。导入套接字 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s、 连接(((“msn

当我在套接字对象上调用
socket.getsockname()
时,它返回我的机器的内部IP和端口的元组。但是,我想检索我的外部IP。最便宜、最有效的方法是什么?

如果没有外部服务器的合作,这是不可能的,因为您和其他计算机之间可能存在任意数量的NAT。如果是自定义协议,您可以要求另一个系统报告它连接到的地址。

我能想到的唯一保证提供给您的方法是点击一个服务来获取它。

导入套接字

s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s、 连接(((“msn.com”,80))


s、 getsockname()

使用源代码中建议的地址

“”
查找您的外部IP地址
'''
导入URL库
进口稀土
def get_ip():
group=re.compile(u'(?P\d+\.\d+\.\d+\.\d+)).search(urllib.URLopener().open('http://jsonip.com/').read()).groupdict()
返回组['ip']
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印获取ip()

您需要使用外部系统来完成此操作

DuckDuckGo的IP答案将以JSON格式为您提供您想要的内容

import requests

def detect_public_ip():
    try:
        # Use a get request for api.duckduckgo.com
        raw = requests.get('https://api.duckduckgo.com/?q=ip&format=json')
        # load the request as json, look for Answer.
        # split on spaces, find the 5th index ( as it starts at 0 ), which is the IP address
        answer = raw.json()["Answer"].split()[4]
    # if there are any connection issues, error out
    except Exception as e:
        return 'Error: {0}'.format(e)
    # otherwise, return answer
    else:
        return answer

public_ip = detect_public_ip()
print(public_ip)

您需要连接到外部服务器,并从响应中获取公共IP


像这样

   import requests

   myPublic_IP = requests.get("http://wtfismyip.com/text").text.strip()

   print("\n[+] My Public IP: "+ myPublic_IP+"\n")

获取公共IP最简单的方法是使用

import requests

IP = requests.get('https://api.ipify.org/').text
print(f'Your IP is: {IP}')

不太清楚为什么会被否决。这是获取任何系统的外部IP地址的唯一方法。仅仅因为路由器告诉你它认为你的IP地址是什么,或者它认为它的IP地址是什么,并不意味着没有另一个NAT。@MatthewSchinckel:+1提及另一个NAT。此外,不同的外部服务可能会看到不同的外部ip,例如,我记得要访问图书馆网站,我必须使用不同的路径,以便网站看到不同的(允许的)ip,即。,公共ip可能取决于目的地。注意,他们说不要每300秒做一次以上。其他提供免费解决方案的伟大网站是从不知道jsonip.com的人那里获得HTTP请求——谢谢。以下是您的答案的“jsonic”变体(自动格式化会弄乱url):json.loads(urllib2.urlopen(urllib2.Request(“”,headers={'Content-Type':'application/json'})).read()['ip']@Rafael Lopes链接被移动到
https://jsonip.com/
(301永久)。顺便说一句,对我来说它工作得很好。在我的例子中,响应中的外部IP是IPv6格式的,这很好。不错。
import requests

def detect_public_ip():
    try:
        # Use a get request for api.duckduckgo.com
        raw = requests.get('https://api.duckduckgo.com/?q=ip&format=json')
        # load the request as json, look for Answer.
        # split on spaces, find the 5th index ( as it starts at 0 ), which is the IP address
        answer = raw.json()["Answer"].split()[4]
    # if there are any connection issues, error out
    except Exception as e:
        return 'Error: {0}'.format(e)
    # otherwise, return answer
    else:
        return answer

public_ip = detect_public_ip()
print(public_ip)
   import requests

   myPublic_IP = requests.get("http://wtfismyip.com/text").text.strip()

   print("\n[+] My Public IP: "+ myPublic_IP+"\n")
import requests

IP = requests.get('https://api.ipify.org/').text
print(f'Your IP is: {IP}')