Basic Analog Call Network program:
1 #Client2 ImportSocket3 4Client =Socket.socket ()5Client.connect (('localhost', 6969))6Client.send (b"Hello ni hao")#only byte codes can be transferred in Python37data = CLIENT.RECV (1024)#1024 Specify the accepted data size to assign the accepted data to the value to data8 Print("recv:", data)9Client.close ()
1 ImportSocket2 3 #Service Side4Server =Socket.socket ()5Server.bind (('localhost', 6969))#bind to listen port6Server.listen ()#Monitor7 Print("I'm going to start waiting for the phone.")8CONN,ADDR = Server.accept ()#wait for the call. Accept returns a value of two conn,addr conn the linked instance client that was generated for the connection and the address of the other link that was created on the server side for the instance addr9 Print("here's the phone.")Tendata = CONN.RECV (1024)#accepts 1024 bytes and assigns the received data to the value One Print("recv:", data) AConn.send (Data.upper ())#returns the received data to the client after a capitalization operation -Server.close ()
Python Network Programming Chapter