问题是,你只是想连接,还是真的想知道地址?
如果你只是想联系,你可以s = socket.create_connection(('mysite.com', 80))
并建立联系。
但是,如果您对该地址感兴趣,可以通过以下方式之一:def get_ip_6(host, port=0):
import socket
# search only for the wanted v6 addresses
result = socket.getaddrinfo(host, port, socket.AF_INET6)
return result # or:
return result[0][4][0] # just returns the first answer and only the addressdef get_ip_6(host, port=0):
# search for all addresses, but take only the v6 ones
alladdr = socket.getaddrinfo(host,port)
ip6 = filter(
lambda x: x[0] == socket.AF_INET6, # means its ip6
alladdr
)
# if you want just the sockaddr
# return map(lambda x:x[4],ip6)
return list(ip6)[0][4][0]