最近工作上接触了python socket,记录一下以备忘记 地址族
socket地址族主要有3类
socket.AF_UNIX
socket.AF_INET
socket.AF_INET6
AF_UNIX
A single string is used for the AF_UNIX address family.用于同一台机器上的进程间通信,一般绑定的是一个路径(这个地址族在代码中python socket中没有发现,不知道为什么)
AF_INET
A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in Internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50.200.5', and port is an integer.
AF_INET6
For AF_INET6 address family, a four-tuple (host, port, flowinfo, scopeid) is used, where flowinfo and scopeid represents sin6_flowinfo and sin6_scope_id member in struct sockaddr_in6 in C. 套接字
套接字类型:
socket.SOCK_STREAM
socket.SOCK_DGRAM
socket.SOCK_RAW
socket.SOCK_RDM
socket.SOCK_SEQPACKET
SOCK_STREAM是有保障的(即能保证数据正确传送到对方)面向连接的SOCKET,用于TCP通信;可以同时有多个数据流。可以想象成一个数据互不干扰的管道。另外一个重要的一点是:数据包的发送和接收是有顺序的。其他一些 Socket 如 UDP、ICMP 和 ARP 没有“连接”的概念,它们是无连接通讯,意味着你可从任何人或者给任何人发送和接收数据包。
比较常用与 SOCK_STREAM 相对的一个类型是 SOCK_DGRAM,它 用于 UDP 通讯协议,UDP 通讯是非连接 Socket,不保证数据的完整性、可靠性,网络环境比较差的情况下,会出现丢包的可能