Socket
Sockets are also commonly referred to as "sockets," which describe IP addresses and ports, and are a handle to a chain of communication, where applications usually make requests to the network through "sockets" or respond to network Requests.
ImportSocketsk=socket.socket () Sk.bind ('127.0.0.1', 9999,))#Set Ip,postSk.listen ()#Monitor whiletrue:conn, Address=sk.accept () conn.sendall (bytes ('Hello world', encoding='Utf-8')) whileTrue:ret= Conn.recv (1024) Ret_str= str (ret, encoding='Utf-8') ifRet_str = ="Q": break Else: Conn.sendall (bytes (ret_str+"Yes", encoding='Utf-8'))Scoket Service Side
ImportSocketobj=socket.socket () Obj.connect ('127.0.0.1', 9999,)) Result= Obj.recv (1024) Re= str (result, encoding='Utf-8')Print(re) whileTRUE:INP= Input ('Please enter the content:') ifINP = ='Q': break Else: Obj.sendall (bytes (inp, encoding='Utf-8')) ret= str (obj.recv (1024x768), encoding='Utf-8') Print(ret) obj.close ()Socket Client
#!/usr/bin/env python#Coding:utf-8ImportSocketdefhandle_request (client): buf= Client.recv (1024) Client.send ("http/1.1 ok\r\n\r\n") Client.send ("Hello, world") defMain (): sock=Socket.socket (socket.af_inet, Socket. Sock_stream) Sock.bind (('localhost', 8080)) Sock.listen (5) whiletrue:connection, Address=sock.accept () handle_request (connection) connection.close ()if __name__=='__main__': Main ()Web Service Apps
Socketserver
The Socketserver internally uses IO multiplexing and "multithreading" and "multi-process" to enable the socket service side to process multiple client requests concurrently. That is, when each client requests a connection to the server, the socket server is creating a "thread" or "process" dedicated to all requests from the current Client.
ImportSocketserverclassMyServer (socketserver. Baserequesthandler):defHANDLE (self): Conn=self.request conn.sendall (bytes ('Hello world', encoding='Utf-8')) whileTrue:ret= Conn.recv (1024) Ret_str= str (ret, encoding='Utf-8') ifRet_str = ="Q": break Else: Conn.sendall (bytes (ret_str+"Yes", encoding='Utf-8'))if __name__=='__main__': Server= Socketserver. Threadingtcpserver (("127.0.0.1", 9999), MyServer) server.serve_forever ()Socketserver
ImportSocketobj=socket.socket () Obj.connect ('127.0.0.1', 9999,)) Result= Obj.recv (1024) Re= str (result, encoding='Utf-8')Print(re) whileTRUE:INP= Input ('Please enter the content:') ifINP = ='Q': break Else: Obj.sendall (bytes (inp, encoding='Utf-8')) ret= str (obj.recv (1024x768), encoding='Utf-8') Print(ret) obj.close ()Client
IO multiplexing
I/O multiplexing refers to the ability to monitor multiple descriptors and, Once a descriptor is ready (usually Read-ready or write-ready), notifies the program to read and write Accordingly.
ImportSocketImportSelectsk=socket.socket () Sk.bind ("127.0.0.1", 8001) ) Sk.listen () inputs=[sk]output=[]message_dict= {} whiletrue:r_list, w_list, e_list= Select.select (inputs, output, inputs, 1) Print("listens on%d Objects"%Len (INPUTS)) forSk_or_conninchr_list:ifSk_or_conn = =sk:conn, Address=sk_or_conn.accept () inputs.append (conn) message_dict[conn]= [] Else: Try: Data= Sk_or_conn.recv (1024) exceptException as Ex:Print(ex) inputs.remove (sk_or_conn)Else: Data_str= str (data, encoding="Utf-8") #sk_or_conn.sendall (bytes (data_str + "good", encoding= "utf-8"))message_dict[sk_or_conn].append (data_str) output.append (sk_or_conn) forConninchOutput:message=message_dict[conn][0]delmessage_dict[conn][0] conn.sendall (bytes (message+"Good", encoding="Utf-8")) output.remove (conn)Server-side
ImportSocketconn=socket.socket () Conn.connect ("127.0.0.1", 8001)) whiletrue:inputs= Input (">>>>>") conn.sendall (bytes (inputs, encoding="Utf-8")) ret= Conn.recv (1024) Print(str (ret, encoding="Utf-8"))Client
Python's socket