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.
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 file:
- The file module is "open", "Read and write" "Close" for a specified document
- The socket module is "open", "Read and write" "Off" for server-side and client sockets
First stage: single-client mode
1 #-*-coding:utf-8-*-2 ImportSocket,os3sk=Socket.socket ()4Sk.bind (('127.0.0.1', 9999))#Start5Sk.listen (5)6 whileTrue:7Conn,address=sk.accept ()#Waiting for client connections8Conn.sendall (Bytes ('Welcome to visit! ', encoding='Utf-8'))9File_size=str (Conn.recv (1024x768), encoding='Utf-8')TenFile_size=Int (file_size) Onehas_recv=0 AConn.sendall (Bytes ('! ', encoding='Utf-8'))#Prevent sticky bags -F=open ('1.jpg','WB') - whilehas_recv!=file_size: theDATA=CONN.RECV (1024) - f.write (data) -Has_recv+=len (data)#Calculate sent bytes -F.close ()Send image Service side
1 #-*-coding:utf-8-*-2 ImportSocket3 ImportOS4 5obj =Socket.socket ()6Obj.connect (('127.0.0.1', 9999))#connecting to the service side7 Print(111)8RET_BYTES=OBJ.RECV (1024)#receive a welcome message9Ret_str=str (ret_bytes,encoding='Utf-8')Ten Print(RET_STR) OneSize=os.stat ('f.jpg'). St_size AObj.sendall (bytes str (size), encoding='Utf-8'))#Send File Size -OBJ.RECV (1024)#Prevent sticky bags -With open ('f.jpg','RB') as F: the forLineinchF: - Obj.sendall (line) -Obj.close ()Send picture Client
Second stage: Multi-client mode, using Select to forge concurrency
1 #io multiplexing2 ImportSocket3 ImportSelect4sk1=Socket.socket ()5Sk1.bind (('127.0.0.1', 8001))6Sk1.listen (1)7inputs=[Sk1,]8outputs=[]9qlist=[]Ten whileTrue: One Print('data being listened to%d'%len (inputs)) ARlist,wlist,elist=select.select (inputs,outputs,[],1) - #rlist for change, wlist incoming what is what, Elist is wrong - forConninchOutputs: theConn.sendall (Bytes (qlist[0]+'Hao', encoding='Utf-8')) - Outputs.remove (conn) - qlist.clear () - forSkinchRlist#Send Message + ifSK = =Sk1: -conn,address=sk.accept () +Conn.sendall (Bytes ('Hello', encoding='Utf-8')) A inputs.append (conn) at Else: - Try:#Receive Message -Result=str (Sk.recv (1024x768), encoding='Utf-8') - qlist.append (Result) - Print(Result) - #sk.sendall (bytes (' Hao ', encoding= ' Utf-8 ')) in exceptException: - Inputs.remove (SK) to Else: Outputs.append (SK)Server Side
1 ImportSocket2 ImportSelect3sk1=Socket.socket ()4Sk1.connect (('127.0.0.1', 8001))5Result=str (Sk1.recv (1024x768), encoding='Utf-8')6 Print(Result)7 whileTrue:8A=input ('>>>')9Sk1.sendall (Bytes (a,encoding='Utf-8'))TenResult=str (Sk1.recv (1024x768), encoding='Utf-8') One Print(Result)Client Side
Phase III: IO multiplexing plus multi-threading, Socketserver module
1 #-*-coding:utf-8-*-2 ImportSocketserver3 classMyserver (socketserver. Baserequesthandler):4 defhandle (self):5 #The handle method is executed whenever a user connects6 #write the code to execute7 if __name__=='__main__':8Server=socketserver. Threadingtcpserver (('127.0.0.1', 8888), Myserver)9Server.serve_forever ()Socketserver
Socket Network Programming