Six. More methods of socket introduction
The server socket function
s.bind() binds (host, port number) to the socket
s.listen() starts TCP listening
s.accept() b passively receives connections from TCP clients, (blocking) waits for connections The arrival of the
client socket function
s.connect() actively initializes the TCP server connection
s.connect_ex() An extended version of the connect() function that returns an error code when an error occurs, instead of throwing an exception
public-purpose socket function
s .recv() receive TCP data
s.send() send TCP data
s.sendall() send TCP data
s.recvfrom() receive UDP data
s.sendto() send UDP data
s.getpeername() connect to the current socket s.getsockname() address of the
current socket
s.getsockopt() returns the parameters of the specified socket
s.setsockopt() sets the parameters of the specified socket
s.close() closes the socket
facing Locked socket methods
s.setblocking() Set the blocking and non-blocking mode of the
socket s.settimeout() Set the timeout for blocking socket operations
s.gettimeout() Get the timeout for blocking socket operations
File-oriented socket functions
s.fineno() The file descriptor of the socket
s.makefile() Create a file
sned associated with the socket And the return value of the sendall method
send() is the number of bytes sent, which may be less than the number of bytes of the string to be sent, which means that it may not be possible to send all the data in the string. If there is an error, an exception will be thrown
sendall() tries to send all the data of the string, returns None if successful, and throws an exception if it fails. So the following two codes are equivalent:
#sock.sendall('Hello world\n')
#buffer = 'Hello world\n'
#while buffer:
# bytes = sock.send(buffer)
# buffer = buffer[bytes :]
7. Verify the legitimacy of the client link
If you want to implement a simple client link authentication function in a distributed system, which is not as complicated as SSL, then use hmac+salt to implement it
Server:
from socket import * import hmac, os secret_key = b'linhaifeng bang bang bang' def conn_auth(conn): # Authenticate client connections print('Start verifying the legitimacy of the new link') msg = os.urandom(32) # random number length conn.sendall(msg) # Send random number length h = hmac.new(secret_key, msg) digest = h.digest() respone = conn.recv(len(digest)) # Encrypted random number length return hmac.compare_digest(respone, digest) def data_handler(conn, bufsize=1024): if not conn_auth(conn): print('The link is invalid, close it') conn.close() return print('The link is valid, start communication') while True: data = conn.recv(bufsize) if not data: break conn.sendall(data.upper()) def server_handler(ip_port, bufsize, backlog=5): tcp_socket_server = socket(AF_INET, SOCK_STREAM) tcp_socket_server.bind(ip_port) tcp_socket_server.listen(backlog) while True: conn, addr = tcp_socket_server.accept() print('New connection (%s:%s)' % (addr[0], addr[1])) data_handler(conn, bufsize)
Client:
from socket import * import hmac,os secret_key = b'linhaifeng bang bang bang' def conn_auth(conn): msg = conn.recv(32) h = hmac.new(secret_key,msg) digest = h.digest() conn.sendall(digest) def client_handler(ip_port,bufsize=1024): tcp_socket_client = socket(AF_INET,SOCK_STREAM) tcp_socket_client.connect(ip_port) conn_auth(tcp_socket_client) while True: data = input('>>>').strip() if not data: continue if data == 'quit': break tcp_socket_client.sendall(data.encode('utf-8')) respone = tcp_socket_client.recv(bufsize) print(respone.decode('utf-8')) tcp_socket_client.close() if __name__ == '__main__': ip_port = ('127.0.0.1',9999) bufsize = 1024 client_handler(ip_port,bufsize)