Sockets are also commonly referred to as a "socket", which describes IP addresses and ports and is a handle to a chain of communication where 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 a file," for files to operate with "open" "Read-write" "Close" mode. Socket is an implementation of this mode, the socket is a special file, some of the socket function is the operation of it (read/write Io, open, closed)
The difference between a socket and a file:
The file module is "open", read-write, or closed for a specified document
The socket module is open, read-write, and shut down for server-side and client sockets
The following is the code for the socket server side
#-*-Coding:utf-8-*-
import socket
#创建ip和端口
ip_port = (' 127.0.0.1 ', 9999)
#创建socket
SK = Socket.socket ()
#绑定ip和端口
sk.bind (ip_port)
#监听 parameter is the maximum number of connections
Sk.listen (5) #最多连接5个 while
True :
print (' Server waiting ... ')
#接受TCP连接并返回 (conn,address), where Conn is a new socket object that can be used to receive and send data. Address is
conn,adrress = sk.accept ()
#输出客户端ip地址
print (' Client IP address: {} '. Format (adrress))
to connect clients Instantiate the received data
Client_data = CONN.RECV (1024)
print (' Client message: ' +client_data ')
#发送消息给客户端
conn.sendall (' How about tonight? ')
#关闭连接
conn.close ()
The following is the code for the socket client
#-*-Coding:utf-8-*-
import socket
#创建ip和端口
ip_port = (' 127.0.0.1 ', 9999)
#创建socket, network communications, TCP
SK = Socket.socket ()
#连接
sk.connect (ip_port)
#发送消息 to service-side
sk.sendall (' Nice to meet you. ')
#接收服务端回复的信息
server_reply = SK.RECV (1024)
print (' Server-side reply message: ' +server_reply ')
#关闭连接
Sk.close ()
1. Socket type
Socket format:
Socket type |
Describe |
Socket.af_unix |
Can only be used for single UNIX system interprocess communication |
Socket.af_inet |
network communication between servers |
Socket.af_inet6 |
IPv6 |
Socket. Sock_stream |
Streaming sockets, for TCP |
Socket. Sock_dgram |
Datagram socket, for UDP |
Socket. Sock_raw |
The original socket, the common socket can not handle ICMP, IGMP and other network messages, and Sock_raw, and secondly, Sock_raw can also handle special IPV4 messages, in addition, the original socket, you can use the IP_HDRINCL socket option to construct IP headers by the user. |
Socket. Sock_seqpacket |
Reliable Continuous Packet Services |
To create a TCP Socket: |
S=socket.socket (Socket.af_inet,socket. SOCK_STREAM) |
To create a UDP Socket: |
S=socket.socket (Socket.af_inet,socket. SOCK_DGRAM)
|
2. Socket function
Note the point:
1 when TCP sends data, TCP connections are established, so no address is required. UDP is intended for connectionless, and each time it is sent to whom it is assigned.
2 server and client can not send the list directly, tuples, dictionaries. Requires a string of repr (data).
Socket function |
Describe |
Service-Side socket function |
S.bind (Address) |
Binds the socket to the address, under Af_inet, in the form of a tuple (host,port). |
S.listen (Backlog) |
Starts listening for TCP incoming connections. Backlog Specifies the maximum number of connections the operating system can suspend before rejecting a connection. The value is at least 1, and most applications are set to 5. |
S.accept () |
Accepts TCP connections and returns (Conn,address), where Conn is a new socket object that can be used to receive and send data. The address is the one that connects the client. |
Client socket function |
S.connect (Address) |
The socket that is connected to the address. The format of the general address is a tuple (hostname,port) and returns a socket.error error if the connection fails. |
S.CONNECT_EX (adddress) |
The function is the same as connect (address), but returns 0 successfully, and the failure returns the value of errno. |
Common socket function |
S.RECV (Bufsize[,flag]) |
Accepts data for a TCP socket. The data is returned as a string, bufsize specifies the maximum amount of data to receive. Flag provides additional information about the message, which can often be ignored. |
S.send (String[,flag]) |
Send TCP data. Sends the data in 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. |
S.sendall (String[,flag]) |
Full TCP data is sent. Sends the data in a string to the attached socket, but attempts to send all the data before returning. Returns none successfully, and throws an exception if it fails. |
S.recvfrom (Bufsize[.flag]) |
Data that accepts UDP sockets. Similar to recv (), but the return value is (data,address). Where data is the string that contains the received information, address is the socket that sends the data. |
S.sendto (string[,flag],address) |
Send UDP data. Sends data to a socket, which is a tuple of form (ipaddr,port), specifying a remote address. The return value is the number of bytes sent. |
S.close () |
Closes the socket. |
S.getpeername () |
Returns the remote address of the connection socket. The return value is usually a tuple (ipaddr,port). |
S.getsockname () |
Returns the socket's own address. is usually a tuple (ipaddr,port) |
S.setsockopt (Level,optname,value) |
Sets the value of the given socket option. |
S.getsockopt (Level,optname[.buflen]) |
Returns the value of the socket option. |
S.settimeout (Timeout) |
Sets the time-out period for a socket operation, timeout is a floating-point number, in seconds. A value of None indicates no time-out period. Typically, the timeout period should be set when the socket is just created, because they may be used for connected operations (such as Connect ()) |