Sockets are also commonly referred to as " sockets ". The two programs on the network realize the exchange of data through a two-way communication connection, one end of this connection is called a socket. The socket is the network connection endpoint. For example, when your Web browser requests a home page on www.fishc.com, your Web browser creates a socket and commands it to connect to the Www.fishc.com Web server host, and the Web server listens on a socket from the request. Both ends use their own sockets to send and receive information.
The socket module provides several functions for working with host names and addresses: gethostname () returns the hostname of the computer on which the program is running:
- #>>> Import Socket
- #>>> Socket.gethostname ()
- # ' Ricardo's computer '
- #>>>
Copy Code
gethostbyname (name) attempts to interpret the given host name as an IP address
- #>>> #socket. gethostbyname (' www.fishc.com ')
- # ' 101.37.44.185 '
- #>>> Socket.gethostbyname (' Ricardo's computer ')
- # ' 192.168.1.104 '
- #>>>
Copy Code
PS: First check whether the current computer can be interpreted. If not, an explanation request is sent to a remote DNS server (the remote DNS server may also forward the interpretation request to another DNS server until the request can be processed). The gethostbyname function returns this IP address or throws an exception after a lookup failure. Expand gethostbyname_ex (name) returns a tuple containing three elements: (a column of the primary host name for the given address, an optional hostname of the same IP address , A list of other IP addresses of the same interface on the same host )
- >>> #>>>socket.gethostbyname_ex (' www.fishc.com ')
- # (' bbgaws7ldwotai4akucb7ckfsnvzoz6m.aliyunwaf.com ', [' www.fishc.com '], [' 101.37.44.185 '])
Copy Code
The gethostbyaddr function works the same as GETHOSTBYNAME_EX, except that the argument you provide to it is an IP address string
The getservbyname (service,protocol) function requires a service name (such as ' Telnet ' or ' ftp ') and a protocol (such as ' TCP ' or ' UDP ') to return the port number used by the service:
- #>>>socket.getservbyname (' http ', ' TCP ')
- #80
- #>>>socket.getservbyname (' telnet ', ' TCP ')
- #23
Copy Code
Socket Communication Import socket
1. Socket ()
- Socket.socket (Family=af_inet, Type=sock_stream, proto=0, Fileno=none)
Copy Code
(1) Family = Af_inet represents communication between servers.
(2) fmily = Af_unix represents the communication between different UNIX processes.
(3) type = Sock_stream represents a TCP connection.
(4) type = Sock_dgram represents a UDP connection.
Ps:2, 3, 4 method for server2. Bind ()
Sk.bind (Address)
Binds the address to the socket. Address (host IP, port number), address must be a tuple.
3. Listen ()
Listen (backlog)
Listens for client connections. The backlog is an optional parameter that represents the maximum number of waiting connections.
4.accept ()
Accept the connection and return (conn,address). Where Conn represents the client's SK object,
Example:
- SK = Socket.socket ()
- Address = (' 127.0.0.1 ', 9080)
- Sk.bind (Address)
- Sk.listen (5)
- CONN,ADDR = Sk.accept ()
- Print (SK)
- Print (conn)
- Print (addr)
- Output Result:
- <socket.socket fd=3, Family=addressfamily.af_inet, Type=socketkind.sock_stream, proto=0, laddr= (' 127.0.0.1 ', 9080 ) >
- <socket.socket fd=4, Family=addressfamily.af_inet, Type=socketkind.sock_stream, proto=0, laddr= (' 127.0.0.1 ', 9080 ), raddr= (' 127.0.0.1 ', 35066) >
- (' 127.0.0.1 ', 35066)
Copy Code
5.recv ()
SK.RECV (bufsize)
Receive data. Where bufsize represents the maximum amount of data that can be received.
6.connect ()
Sk.connect (Address)
Connect the socket to the specified address. Address is represented by a tuple.
7.send ()
Sk.send (data)
Send data to the connected socket.
8.sendall ()
Like send, the internal calls the Send () method recursively, attempting to send all the data.
9.sendto ()
Sk.sendto (DATA,ADDR) to specify a remote address
notice! TCP (SOCK_STREAM), need to connect to the remote host to send data, send data with send (b ' character ') UDP (SOCK_DGRAM), do not need to connect, send data directly, send data with sendto (b ' character ', (Ip,port))
10.settimeout (Timeout)
Sets the timeout.
11.getpeername ()
Returns the address of the remote socket (address,port).
12.getsockname ()
Return your own socket address (address,port).
13.fileno ()
The file descriptor for the socket.
Example: Using the socket module for the Communication Service side:
- SK = Socket.socket ()
- Address = (' 127.0.0.1 ', 9080)
- Sk.bind (Address)
- Sk.listen (5)
- CONN,ADDR = Sk.accept ()
- data = CONN.RECV (1024)
- Print (str (data, ' UTF8 '))
Copy Code
Client:
- SK = Socket.socket ()
- Address = (' 127.0.0.1 ', 9080)
- Sk.connect (Address)
- INP = input (' >> ')
- Sk.send (Bytes (INP, ' UTF8 '))
Copy Code
The "socket" of the Python standard library