The socket (socket) starts with UNIX, which is what people call BSD Unix. There are two types of socket families: file-system-based and network-based. The first is based on the file system, the address family is expressed as: Af_unix (or af_local), the second is a network socket, is network-based, the address family is represented as Af_inet (Af_inet6 represents IPv6). Added support for a Linux socket in Python 2.5: af_netlink (no connection [see below]) The socket family allows the IPC between user code and kernel code to use the standard BSD socket interface. Python only supports Af_unix,af_netlink, and the Af_inet family. The most widely used one is described here: af_inet.
Depending on the type of socket, it can be divided into connection-oriented and non-connected.
Connection-oriented, a connection needs to be established before communication, which provides sequential, reliable, and not repetitive data transfer, and is not added to the boundary. The primary protocol for implementing this connection is the Transmission Control Protocol (TCP), which corresponds to a socket type of sock_stream. Sockets use Internet Protocol (IP) to locate hosts in the network, the TCP/IP protocol, to support connection-oriented sockets.
No connection, no need to establish a connection to communicate. The primary protocol that implements this connection is the User Datagram Protocol (that is, UDP), which specifies that the socket type is SOCK_DGRAM. Sockets use the Internet Protocol to find hosts in the network, the UDP/IP protocol, to support non-connected sockets.
1. Socket () module function
The syntax for creating a socket () socket is as follows:
Sockets (socket_family, Socket_type, protocol=0)
Socket_family generally Af_unix or af_inet,socket_type can be sock_stream or sock_dgram,protocol generally not filled, the default is 0.
Because there are too many properties in the socket module, the "from socket Import *" is used here to reduce the code length.
Create a socket for TCP/IP:
Tcpsocket = socket (af_inet, SOCK_STREAM)
Similarly, create a UDP/IP socket:
Udpsocket = socket (af_inet, SOCK_STREAM)
2. Socket object (built-in) method
The common socket object functions in Python are as follows:
3. The C/S model based on TCP
Python Network Programming Basics