socekt.getaddrinfo的返回值介绍
- family: 表示socket使用的协议簇。常用的协议簇包括AF_UNIX(本机通信)/AF_INET(TCP/IP协议簇中的IPv4协议)/AF_INET6(TCP/IP协议簇中的IPv4协议)。在python的socket包中,用1表示AF_UNIX,2表示AF_INET,10表示AF_INET6。
- sockettype:表示socket的类型。常见的socket类型包括SOCK_STREAM(TCP流)/SOCK_DGRAM(UDP数据报)/SOCK_RAW(原始套接字)。其中,SOCK_STREAM=1,SOCK_DGRAM=2,SOCK_RAW=3
- proto:顾名思义,就是指定协议。套接口所用的协议。如调用者不想指定,可用0。常用的协议有,IPPROTO_TCP(=6)和IPPTOTO_UDP(=17),它们分别对应TCP传输协议、UDP传输协议。
import socket,sys
host,port= sys.argv[1:]
results = socket.getaddrinfo(host,port,0,socket.SOCK_STREAM)
for result in results:
print ("-" * 60)
#Print whether we got back an IPv4 or IPv6
if result[0] == socket.AF_INET:
print ("Family:AF_INET")
elif result[0] ==socket.AF_INET6:
print ("Family:AF_INET6")
else:
print ("Family:",result[0])
if result[1] == socket.SOCK_STREAM:
print ("Socket Type:SOCK_STREAM")
elif result[1] == socket.SOCK_DGRAM:
print ("Socket Type: SOCK_DGRAM")
print ("Protocol:",result[2])
print ("Canonical Name:",result[3])
print ("Socket Address:",result[4])
参数输入:baidu.com http
结果输出:
------------------------------------------------------------
Family:AF_INET
------------------------------------------------------------
Family:AF_INET
------------------------------------------------------------
Family:AF_INET
Socket Type:SOCK_STREAM
Protocol: 0
Canonical Name:
Socket Address: ('220.181.111.86', 80)