A: What is a socket
Sockets are also commonly referred to as "sockets", which describe IP addresses and ports, are a handle to a communication chain, and applications typically send requests to the network via "sockets" or answer network requests.
Sockets originate from UNIX, and one of the basic philosophies of unix/linux is "Everything is file", and the file is operated with "open" "Read and Write" "Off" mode. Socket is an implementation of this pattern, the socket is a special kind of file, some of the socket function is the operation of it (read/write Io, open, close)
The difference between a socket and a file
1) The file module is opened, read/write, closed for a specified
2) Socket module is open, read/write, shut down for server side and client socket
Second: the socket interaction process
Three: Description
Server-side
1) Build Socket object:socket.socket (Socket.af_inet,socket. sock_stream,0)
Parameter 1: Address family
Socket.af_inet (Default IPV4)
Socket.af_inet6 (IPV6)
Socket.af_unix can only be used for inter-process communication between UNIX systems
Parameter 2: Type
Socket. Sock_stream Streaming socket for TCP (default)
Socket. SOCK_STREAM Datagram socket for UDP
Socket. Sock_raw the original socket, the ordinary socket can not handle ICMP, IGMP and other network messages, and Sock_raw may;
Second, Sock_raw can also handle special IPV4 messages, and with the original sockets, the IP header can be constructed by the user through the IP_HDRINCL socket option.
Socket. SOCK_RDM is a reliable form of UDP that guarantees the delivery of datagrams but does not guarantee the order.
Sock_ram is used to provide low-level access to the original protocol, which is used when certain special operations are required, such as sending ICMP packets. Sock_ram is typically used only by advanced users or by programs that are run by administrators.
Socket. Sock_seqpacket Reliable Continuous Packet service
Parameter 3: protocol
0 (default) protocol associated with a particular address family, if 0, the system automatically chooses an appropriate protocol based on the address format and socket category.
2) bind (address)
Binds a socket to an address. The address format depends on the address family, which is represented as a no-group (host,port) Form in Af_inet
3) Listen (backlog)
Start listening for incoming connections. The backlog specifies the maximum number of connections that can be suspended before a connection is rejected. That is, the multi-backlog client is most able to link, more than this number will be error.
The backlog equals 5, indicating that the kernel has received a connection request, but the server has not yet called the accept to process the maximum number of connections is 5
This value cannot be infinitely large because the connection queue is maintained in the kernel
4) setblocking (BOOL)
Whether blocking (default true), if set to false, then the accept and recv when there is no data, the error. Because the default is true, you don't have to write
5) Accept ()
Accepts the connection and returns (Conn,address), where Conn is a new socket object that can be used to receive and send data. Address is the location of the connection client
Customer Service side
Socket
Connect (address)
The socket that is connected to the address. Generally, address is in the form of a tuple (Hostname,port) and returns a socket.error error if there is an error in the connection.
CONNECT_EX (Address)
Ibid., except that there will be a return value, return 0 when the connection succeeds, and return the encoding when the connection fails
Other
1) Close ()
Close socket
2) recv (Bufsize[,flag])
Accepts the data for the socket. The data is returned as a string, and bufsize specifies the maximum quantity that can be received. Flag provides additional information about the message, which can usually be ignored.
3) Recvfrom (Bufsize[.flag])
Similar to recv (), but the return value is (data,address). Where data is the string that contains the received information, address is the socket addressing that sent the data.
4) Send (String[,flag])
Sends data from a string to a connected socket. The return value is the number of bytes to send, which may be less than the byte size of the string. That is, the specified content may not be sent all.
5) Sendall (String[,flag])
Sends data from a string to a connected socket, but attempts to send all data before returning. Successful return none, Failure throws an exception.
Internally, the send is called recursively, sending all the content.
6) sendto (string[,flag],address)
Sends the data to the socket, address is a tuple in the form of (Ipaddr,port), specifying the remote address. The return value is the number of bytes sent. This function is mainly used for UDP protocol.
7) settimeout (timeout)
Sets the timeout period for the socket operation, and timeout is a floating-point number in seconds. A value of None indicates no over-time. In general, hyper-times should be set when a socket is just created, as they may be used for connected operations (such as client connections waiting up to 5s)
8) Getpeername ()
Returns the remote address of the connection socket. The return value is typically a tuple (ipaddr,port).
9) getsockname ()
Returns the socket's own address. Typically a tuple (ipaddr,port)
Ten) Fileno ()
File descriptor for sockets
Python Basic Learning Log Day8-socket