这里写自定义目录标题
一、 socket 的创建
1、使用示例
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
2、源码
socket(…) 的源码
def __init__(self, family=-1, type=-1, proto=-1, fileno=None):
# For user code address family and type values are IntEnum members, but
# for the underlying _socket.socket they're just integers. The
# constructor of _socket.socket converts the given argument to an
# integer automatically.
if fileno is None:
if family == -1:
family = AF_INET
if type == -1:
type = SOCK_STREAM
if proto == -1:
proto = 0
...省略三行...
二、参数说明
1、python 3.8 family的取值
AF_APPLETALK = 16 # AppleTalk
AF_DECnet = 12 # DECet protocol sockets
AF_INET = 2 # IPv4 Internet protocols
AF_INET6 = 23 # IPv6 Internet protocols
AF_IPX = 6 # Novell protocols
AF_IRDA = 26 # Socket interface over IrDA (moved to staging in Linux 4.14, removed in Linux 4.17).
AF_SNA = 11
AF_UNSPEC = 0
2、python 3.8 type的取值
SOCK_DGRAM = 2
SOCK_RAW = 3
SOCK_RDM = 4
SOCK_SEQPACKET = 5
SOCK_STREAM = 1
- SOCK_STREAM
提供有序、可靠、双向、基于连接的字节流。可以支持带外数据传输机制。 - SOCK_DGRAM
支持数据报(无连接、不可靠、最大长度固定的消息)。 - SOCK_RAW
提供原始网络协议访问。 - SOCK_RDM
提供一个不保证排序的可靠数据报层。 - SOCK_SEQPACKET
为固定最大长度的数据报提供有序、可靠、基于双向连接的数据传输路径;使用者需要用每个输入系统调用读取整个数据包。
3、python 3.8 proto的取值,此值称为 Assigned Internet Protocol Numbers
IPPROTO_AH = 51
IPPROTO_CBT = 7
IPPROTO_DSTOPTS = 60
IPPROTO_EGP = 8
IPPROTO_ESP = 50
IPPROTO_FRAGMENT = 44
IPPROTO_GGP = 3
IPPROTO_HOPOPTS = 0
IPPROTO_ICLFXBM = 78
IPPROTO_ICMP = 1
IPPROTO_ICMPV6 = 58
IPPROTO_IDP = 22
IPPROTO_IGMP = 2
IPPROTO_IGP = 9
IPPROTO_IP = 0
IPPROTO_IPV4 = 4
IPPROTO_IPV6 = 41
IPPROTO_L2TP = 115
IPPROTO_MAX = 256
IPPROTO_ND = 77
IPPROTO_NONE = 59
IPPROTO_PGM = 113
IPPROTO_PIM = 103
IPPROTO_PUP = 12
IPPROTO_RAW = 255
IPPROTO_RDP = 27
IPPROTO_ROUTING = 43
IPPROTO_SCTP = 132
IPPROTO_ST = 5
IPPROTO_TCP = 6
IPPROTO_UDP = 17
- 根据 2023-11-07最新 iana 的数据,已经批准的 protocal number 如下:
Decimal Keyword Protocol IPv6 Extension Reference
Header
0 HOPOPT IPv6 Hop-by-Hop Option Y [RFC8200]
1 ICMP Internet Control Message [RFC792]
2 IGMP Internet Group Management [RFC1112]
3 GGP Gateway-to-Gateway [RFC823]
4 IPv4 IPv4 encapsulation [RFC2003]
5 ST Stream [RFC1190][RFC1819]
6 TCP Transmission Control [RFC9293]
7 CBT CBT [Tony_Ballardie]
8 EGP Exterior Gateway Protocol [RFC888][David_Mills]
any private interior gateway
9 IGP (used by Cisco for their [Internet_Assigned_Numbers_Authority]
IGRP)
10 BBN-RCC-MON BBN RCC Monitoring [Steve_Chipman]
11 NVP-II Network Voice Protocol [RFC741][Steve_Casner]
[Boggs, D., J. Shoch, E. Taft, and R. Metcalfe, "PUP: An Internetwork
12 PUP PUP Architecture", XEROX Palo Alto Research Center, CSL-79-10, July 1979;
also in IEEE Transactions on Communication, Volume COM-28, Number 4,
April 1980.][[XEROX]]
13 ARGUS (deprecated) ARGUS [Robert_W_Scheifler]
14 EMCON EMCON [Bich_Nguyen]
15 XNET Cross Net Debugger [Haverty, J., "XNET Formats for Internet Protocol Version 4", IEN 158,
October 1980.][Jack_Haverty]
16 CHAOS Chaos [J_Noel_Chiappa]
17 UDP User Datagram [RFC768][Jon_Postel]
18 MUX Multiplexing [Cohen, D. and J. Postel, "Multiplexing Protocol", IEN 90,
USC/Information Sciences Institute, May 1979.][Jon_Postel]
19 DCN-MEAS DCN Measurement Subsystems [David_Mills]
20 HMP Host Monitoring [RFC869][Bob_Hinden]
21 PRM Packet Radio Measurement [Zaw_Sing_Su]
["The Ethernet, A Local Area Network: Data Link Layer and Physical Layer
Specification", AA-K759B-TK, Digital Equipment Corporation, Maynard, MA.
Also as: "The Ethernet - A Local Area Network", Version 1.0, Digital
Equipment Corporation, Intel Corporation, Xerox Corporation, September
22 XNS-IDP XEROX NS IDP 1980. And: "The Ethernet, A Local Area Network: Data Link Layer and
Physical Layer Specifications", Digital, Intel and Xerox, November 1982.
And: XEROX, "The Ethernet, A Local Area Network: Data Link Layer and
Physical Layer Specification", X3T51/80-50, Xerox Corporation, Stamford,
CT., October 1980.][[XEROX]]
23 TRUNK-1 Trunk-1 [Barry_Boehm]
24 TRUNK-2 Trunk-2 [Barry_Boehm]
25 LEAF-1 Leaf-1 [Barry_Boehm]
26 LEAF-2 Leaf-2 [Barry_Boehm]
27 RDP Reliable Data Protocol [RFC908][Bob_Hinden]
28 IRTP Internet Reliable [RFC938][Trudy_Miller]
Transaction
29 ISO-TP4 ISO Transport Protocol Class [RFC905][Robert_Cole]
4
30 NETBLT Bulk Data Transfer Protocol [RFC969][David_Clark]
MFE Network Services [Shuttleworth, B., "A Documentary of MFENet, a National Computer
31 MFE-NSP Protocol Network", UCRL-52317, Lawrence Livermore Labs, Livermore, California,
June 1977.][Barry_Howard]
32 MERIT-INP MERIT Internodal Protocol [Hans_Werner_Braun]
33 DCCP Datagram Congestion Control [RFC4340]
Protocol
34 3PC Third Party Connect Protocol [Stuart_A_Friedberg]
35 IDPR Inter-Domain Policy Routing [Martha_Steenstrup]
Protocol
36 XTP XTP [Greg_Chesson]
37 DDP Datagram Delivery Protocol [Wesley_Craig]
38 IDPR-CMTP IDPR Control Message [Martha_Steenstrup]
Transport Proto
39 TP++ TP++ Transport Protocol [Dirk_Fromhein]
40 IL IL Transport Protocol [Dave_Presotto]
41 IPv6 IPv6 encapsulation [RFC2473]
42 SDRP Source Demand Routing [Deborah_Estrin]
Protocol
43 IPv6-Route Routing Header for IPv6 Y [Steve_Deering]
44 IPv6-Frag Fragment Header for IPv6 Y [Steve_Deering]
45 IDRP Inter-Domain Routing [Sue_Hares]
Protocol
46 RSVP Reservation Protocol [RFC2205][RFC3209][Bob_Braden]
47 GRE Generic Routing [RFC2784][Tony_Li]
Encapsulation
48 DSR Dynamic Source Routing [RFC4728]
Protocol
49 BNA BNA [Gary Salamon]
50 ESP Encap Security Payload Y [RFC4303]
51 AH Authentication Header Y [RFC4302]
52 I-NLSP Integrated Net Layer [K_Robert_Glenn]
Security TUBA
53 SWIPE (deprecated) IP with Encryption [John_Ioannidis]
54 NARP NBMA Address Resolution [RFC1735]
Protocol
55 Min-IPv4 Minimal IPv4 Encapsulation [RFC2004][Charlie_Perkins]
Transport Layer Security
56 TLSP Protocol using Kryptonet key [Christer_Oberg]
management
57 SKIP SKIP [Tom_Markson]
58 IPv6-ICMP ICMP for IPv6 [RFC8200]
59 IPv6-NoNxt No Next Header for IPv6 [RFC8200]
60 IPv6-Opts Destination Options for IPv6 Y [RFC8200]
61 any host internal protocol [Internet_Assigned_Numbers_Authority]
62 CFTP CFTP [Forsdick, H., "CFTP", Network Message, Bolt Beranek and Newman, January
1982.][Harry_Forsdick]
63 any local network [Internet_Assigned_Numbers_Authority]
64 SAT-EXPAK SATNET and Backroom EXPAK [Steven_Blumenthal]
65 KRYPTOLAN Kryptolan [Paul Liu]
66 RVD MIT Remote Virtual Disk [Michael_Greenwald]
Protocol
67 IPPC Internet Pluribus Packet [Steven_Blumenthal]
Core
68 any distributed file system [Internet_Assigned_Numbers_Authority]
69 SAT-MON SATNET Monitoring [Steven_Blumenthal]
70 VISA VISA Protocol [Gene_Tsudik]
71 IPCV Internet Packet Core Utility [Steven_Blumenthal]
72 CPNX Computer Protocol Network [David Mittnacht]
Executive
73 CPHB Computer Protocol Heart Beat [David Mittnacht]
74 WSN Wang Span Network [Victor Dafoulas]
75 PVP Packet Video Protocol [Steve_Casner]
76 BR-SAT-MON Backroom SATNET Monitoring [Steven_Blumenthal]
77 SUN-ND SUN ND PROTOCOL-Temporary [William_Melohn]
78 WB-MON WIDEBAND Monitoring [Steven_Blumenthal]
79 WB-EXPAK WIDEBAND EXPAK [Steven_Blumenthal]
80 ISO-IP ISO Internet Protocol [Marshall_T_Rose]
81 VMTP VMTP [Dave_Cheriton]
82 SECURE-VMTP SECURE-VMTP [Dave_Cheriton]
83 VINES VINES [Brian Horn]
84 IPTM Internet Protocol Traffic [Jim_Stevens][1]
Manager
85 NSFNET-IGP NSFNET-IGP [Hans_Werner_Braun]
86 DGP Dissimilar Gateway Protocol [M/A-COM Government Systems, "Dissimilar Gateway Protocol Specification,
Draft Version", Contract no. CS901145, November 16, 1987.][Mike_Little]
87 TCF TCF [Guillermo_A_Loyola]
88 EIGRP EIGRP [RFC7868]
89 OSPFIGP OSPFIGP [RFC1583][RFC2328][RFC5340][John_Moy]
[Welch, B., "The Sprite Remote Procedure Call System", Technical Report,
90 Sprite-RPC Sprite RPC Protocol UCB/Computer Science Dept., 86/302, University of California at Berkeley,
June 1986.][Bruce Willins]
91 LARP Locus Address Resolution [Brian Horn]
Protocol
92 MTP Multicast Transport Protocol [Susie_Armstrong]
93 AX.25 AX.25 Frames [Brian_Kantor]
94 IPIP IP-within-IP Encapsulation [John_Ioannidis]
Protocol
95 MICP (deprecated) Mobile Internetworking [John_Ioannidis]
Control Pro.
96 SCC-SP Semaphore Communications [Howard_Hart]
Sec. Pro.
97 ETHERIP Ethernet-within-IP [RFC3378]
Encapsulation
98 ENCAP Encapsulation Header [RFC1241][Robert_Woodburn]
99 any private encryption [Internet_Assigned_Numbers_Authority]
scheme
100 GMTP GMTP [[RXB5]]
101 IFMP Ipsilon Flow Management [Bob_Hinden][November 1995, 1997.]
Protocol
102 PNNI PNNI over IP [Ross_Callon]
103 PIM Protocol Independent [RFC7761][Dino_Farinacci]
Multicast
104 ARIS ARIS [Nancy_Feldman]
105 SCPS SCPS [Robert_Durst]
106 QNX QNX [Michael_Hunter]
107 A/N Active Networks [Bob_Braden]
108 IPComp IP Payload Compression [RFC2393]
Protocol
109 SNP Sitara Networks Protocol [Manickam_R_Sridhar]
110 Compaq-Peer Compaq Peer Protocol [Victor_Volpe]
111 IPX-in-IP IPX in IP [CJ_Lee]
112 VRRP Virtual Router Redundancy [RFC5798]
Protocol
113 PGM PGM Reliable Transport [Tony_Speakman]
Protocol
114 any 0-hop protocol [Internet_Assigned_Numbers_Authority]
115 L2TP Layer Two Tunneling Protocol [RFC3931][Bernard_Aboba]
116 DDX D-II Data Exchange (DDX) [John_Worley]
117 IATP Interactive Agent Transfer [John_Murphy]
Protocol
118 STP Schedule Transfer Protocol [Jean_Michel_Pittet]
119 SRP SpectraLink Radio Protocol [Mark_Hamilton]
120 UTI UTI [Peter_Lothberg]
121 SMP Simple Message Protocol [Leif_Ekblad]
122 SM (deprecated) Simple Multicast Protocol [Jon_Crowcroft][draft-perlman-simple-multicast]
123 PTP Performance Transparency [Michael_Welzl]
Protocol
124 ISIS over IPv4 [Tony_Przygienda]
125 FIRE [Criag_Partridge]
126 CRTP Combat Radio Transport [Robert_Sautter]
Protocol
127 CRUDP Combat Radio User Datagram [Robert_Sautter]
128 SSCOPMCE [Kurt_Waber]
129 IPLT [[Hollbach]]
130 SPS Secure Packet Shield [Bill_McIntosh]
131 PIPE Private IP Encapsulation [Bernhard_Petri]
within IP
132 SCTP Stream Control Transmission [Randall_R_Stewart]
Protocol
133 FC Fibre Channel [Murali_Rajagopal][RFC6172]
134 RSVP-E2E-IGNORE [RFC3175]
135 Mobility Header Y [RFC6275]
136 UDPLite [RFC3828]
137 MPLS-in-IP [RFC4023]
138 manet MANET Protocols [RFC5498]
139 HIP Host Identity Protocol Y [RFC7401]
140 Shim6 Shim6 Protocol Y [RFC5533]
141 WESP Wrapped Encapsulating [RFC5840]
Security Payload
142 ROHC Robust Header Compression [RFC5858]
143 Ethernet Ethernet [RFC8986]
144 AGGFRAG AGGFRAG encapsulation [RFC9347]
payload for ESP
145 NSH Network Service Header N [RFC9491]
146-252 Unassigned [Internet_Assigned_Numbers_Authority]
253 Use for experimentation and Y [RFC3692]
testing
254 Use for experimentation and Y [RFC3692]
testing
255 Reserved [Internet_Assigned_Numbers_Authority]
三、参考资料
-
AF - Address Families:
https://www.man7.org/linux/man-pages/man7/address_families.7.html -
Socket Types
https://www.man7.org/linux/man-pages/man2/socket.2.html -
Protocal Numbers
https://www.iana.org/assignments/protocol-numbers/protocol-numbers.txt
https://www.man7.org/linux/man-pages/man7/raw.7.html