The server and the client communicate, can be two computers, one as a server, one as a client. can also be tested on a single computer, IP can be used 127.0.0.1. Or use a virtual machine as one of them. IP can be viewed ipconfig.
Importsocket# Service-side phone= Socket.socket (socket.af_inet,socket. SOCK_STREAM)#based on network communication, TCP protocolPhone.bind (('IP', port))#Binding IPPhone.listen (5) conn,addr= Phone.accept ()#Link, addressmsg = CONN.RECV (1024x768). Decode ('Utf-8')Print(msg) conn.send ('I'm fine.'. Encode ('Utf-8')) Conn.close () phone.close ()
ImportSocket#ClientPhone=Socket.socket (Socket.af_inet,socket. Sock_stream) Phone.connect (('IP', port))#Dial PhonePhone.send ('how you doing?'. Encode ('Utf-8'))#Send MessageDATA=PHONE.RECV (1024x768). Decode ('Utf-8')Print('received a message from the server:', data)
Python base socket