@1: Synchronous network programming (i.e. blocking mode)
Synchronous network programming can only connect one client at a time.
Server side:
ImportSocketdefdebugprint (name, value):Print("{0}: {1}". Format (name, value))defserver ():#1:Server =Socket.socket ()#note:not "host = Server.gethostname ()"Host =socket.gethostname () port= 8080#2: #Note:not "Server.bind (host, Port)" Typeerror:bind () takes exactly one argument (2 given)Server.bind ((host, port))#3:Server.listen (3)#Listen () parameter is the number of connections that are allowed to wait while1: #4: Client, address = server.accept ()#the Accept () will block until a client connectsDebugPrint ("Client", client) DebugPrint ("Address", address)#5: Client. Send ("welcome!") Client.close ()defMain (): Server ()
Client side:
ImportSocketImportServerdefclient ():#1:Client =Socket.socket () host=socket.gethostname () port= 8080#2:Client.connect ((host, port))#3:Content = CLIENT.RECV (1024) Server.debugprint ("From Server", content)defMain (): Client ()
@2: Asynchronous network programming (i.e. non-blocking mode)
Asynchronous network programming, allowing multiple client links.
There are 3 implementations of asynchronous network programming: Fork (multi-process), multi-threading, asynchronous IO
Fork mode occupies the resources, Windows does not support bifurcation, multi-threaded mode has synchronization problems;
Python Network Programming