通常是AF_INET, IPV6就是AF_INET6python
TCP選SOCK_STREAM,UDP選SOCK_DGRAM緩存
Socket()接口的第三個參數有用到,通常填0,具體含義暫時不知道,好像是使用TCP仍是UDP協議服務器
全部的套接字接口都會必定會用到這幾個參數,例如socket.socket 和bind,就須要五個類型的參數設定了。app
>>> from socket import getaddrinfo >>> infolist = socket.getaddrinfo('gatech.edu', 'www') >>> pprint(infolist) [(2, 1, 6, '', ('130.207.244.244', 80)), #獲得了 (2, 2, 17, '', ('130.207.244.244', 80))] >>> ftpca = infolist[0] >>> ftpca[0:3] (2, 1, 6) >>> s = socket.socket(*ftpca[0:3]) >>> ftpca[4] ('130.207.244.244', 80) >>> s.connect(ftpca[4])
ftpca 是一個縮寫,表示」family, type, protocol, canonicalname, and address」socket
這樣能夠直接獲取目標的配置信息,不須要進行額外的判斷,直接進行鏈接操做。tcp
如下來自 python331.chmthis
接口聲明:socket.getaddrinfo(host, port, family=0, type=0, proto=0, flags=0)spa
接口返回值: (family, type, proto, canonname, socketname)code
socket.AF_UNIXorm
socket.AF_INET
socket.AF_INET6
These constants represent the address (and protocol) families, used for the first argument to socket(). If the AF_UNIX constant is not defined then this protocol is unsupported. More constants may be available depending on the system.
socket.SOCK_STREAM
socket.SOCK_DGRAM
socket.SOCK_RAW
socket.SOCK_RDM
socket.SOCK_SEQPACKET
These constants represent the socket types, used for the second argument to socket(). More constants may be available depending on the system. (Only SOCK_STREAM and SOCK_DGRAM appear to be generally useful.)
指定應用程序所使用的通訊協議。此參數能夠指定單個協議系列中的不一樣傳輸協議。在Internet通信域中,此參數通常取值爲0,系統會根據套接字的類型(上一個參數類型)決定應使用的傳輸層協議。
在VC++裏面的類型有(ws2def.h)
#define IPPROTO_IP 0 /* dummy for IP */ #define IPPROTO_ICMP 1 /* control message protocol */ #define IPPROTO_IGMP 2 /* internet group management protocol */ #define IPPROTO_GGP 3 /* gateway^2 (deprecated) */ #define IPPROTO_TCP 6 /* tcp */ #define IPPROTO_PUP 12 /* pup */ #define IPPROTO_UDP 17 /* user datagram protocol */ #define IPPROTO_IDP 22 /* xns idp */ #define IPPROTO_ND 77 /* UNOFFICIAL net disk proto */ #define IPPROTO_RAW 255 /* raw IP packet */ #define IPPROTO_MAX 256
Python暫時不知道,socket.SOL_UDP
在VC++中的定義和說明(ws2def.h)
#define AI_PASSIVE 0x00000001 // Socket address will be used in bind() call #define AI_CANONNAME 0x00000002 // Return canonical name in first ai_canonname #define AI_NUMERICHOST 0x00000004 // Nodename must be a numeric address string #define AI_NUMERICSERV 0x00000008 // Servicename must be a numeric port number #define AI_ALL 0x00000100 // Query both IP6 and IP4 with AI_V4MAPPED #define AI_ADDRCONFIG 0x00000400 // Resolution only if global address configured #define AI_V4MAPPED 0x00000800 // On v6 failure, query v4 and convert to V4MAPPED format #define AI_NON_AUTHORITATIVE 0x00004000 // LUP_NON_AUTHORITATIVE #define AI_SECURE 0x00008000 // LUP_SECURE #define AI_RETURN_PREFERRED_NAMES 0x00010000 // LUP_RETURN_PREFERRED_NAMES #define AI_FQDN 0x00020000 // Return the FQDN in ai_canonname #define AI_FILESERVER 0x00040000 // Resolving fileserver name resolution
搜尋域名對應的socketName時,通常先查找本地的hosts或者相應的緩存,若是上面兩個都沒有找到的話,那就經過DNS查找,經過DNS查找比較慢。第一次訪問的時候速度比較慢,後面因爲有了相應的緩 存,而且沒有失效的話,那就不會經過DNS查找了。
Default port: 53
Libraries: PyDNS, dnspythonk, python3用不了
使用上面的兩個第三方庫,能夠經過查找DNS服務,查找MX(Mail Exchanger,郵件交換)記錄來獲得郵件地址所對應的郵件服務器的地址。
當缺失DNS服務器時,可使用另外兩種技術來獲取域名對應的socketname, Zeroconf 和 Dynamic DNS。
>>> import socket >>> socket.has_ipv6 True
>>> import socket >>> dir(socket) ['AF_APPLETALK', 'AF_DECnet', 'AF_INET'…