Learn python using Python for network programming, write a simple client and server-side communication, most of the content from the Web tutorial, here to summarize for later review.
Let's introduce the three-time handshake for TCP:
1, simply send a message:
Server-side:
Import== ("127.0.0.1", 8888) Sk.bind (ip_port) Sk.listen ( 5)print(" waiting to receive data ... " ="Hello world! " Conn.send (Msg.encode ()) Conn.close ()
Client:
Import== ("127.0.0.1", 8888= client.recv (1024x768) Print(Data.decode ())
There is no continuous sending of messages, and the program terminates once the connection is sent.
2.1, Continuous Send message (TCP mode )
Server-side:
ImportSocketImportRandomsk=socket.socket () Ip_port= ("127.0.0.1", 8888) Sk.bind (ip_port) Sk.listen (5) whileTrue:Print("waiting for data to be accepted ...") conn, address=sk.accept () msg="Hello world!"Conn.send (Msg.encode ()) whileTrue:data=CONN.RECV (1024) Print(Data.decode ())ifData==b'Exit': Breakconn.send (data) conn.send (str (Random.randint (1,1000) ). Encode ()) Conn.close ()
Client:
Importsocketclient=socket.socket () Ip_port= ("127.0.0.1", 8888) Client.connect (ip_port) whileTrue:data= CLIENT.RECV (1024) Print(Data.decode ()) Msg_input=input ("Please input mesage to send ...") Client.send (Msg_input.encode ())ifmsg_input== b'Exit': BreakData= CLIENT.RECV (1024) Print(Data.decode ())
Use the while function to send messages consecutively.
2.2, Continuous Send message (UDP mode )
Server-side:
Import Socket# defines the UDP mode sk=socket.socket (socket.af_inet,socket. SOCK_DGRAM) Ip_port= ("127.0.0.1", 8888) Sk.bind (ip_port) while True: data=sk.recv (1024x768) print(Data.decode ())
Client:
#UDPImportSocketsk=Socket.socket (Socket.af_inet,socket. SOCK_DGRAM) Ip_port=("127.0.0.1", 8888) whileTrue:msg_input=input ("Please input message to send ...") ifMsg_input==b'Exit': Breaksk.sendto (Msg_input.encode (), Ip_port) sk.close ()
3, multi-client Send message (TCP mode)
Server-side:
#TCPImportSocketserverImportRandomclassMyServer (socketserver. Baserequesthandler):defSetup (self):Pass defhandle (self): conn=self.request msg="Hello world!"Conn.send (Msg.encode ()) whileTrue:data= CONN.RECV (1024) Print(Data.decode ())ifdata== b'Exit': Breakconn.send (data) conn.send (str (Random.randint (1,1000) ). Encode ()) Conn.close ()defFinish (self):Passif __name__=="__main__": Server=socketserver. Threadingtcpserver (("127.0.0.1", 8888), MyServer) Server.serve_forever ()
Client:
#TCPImportsocketclient=socket.socket () Ip_port= ("127.0.0.1", 8888) Client.connect (ip_port) whileTrue:data= CLIENT.RECV (1024) Print(Data.decode ()) Msg_input=input ("Please input mesage to send ...") Client.send (Msg_input.encode ())ifmsg_input== b'Exit': BreakData= CLIENT.RECV (1024) Print(Data.decode ())
After starting the server side, the server side waits for the client program to connect, we can start multiple clients to connect to the server side.
Note: The content originates from the Internet
Python network programming-socket simple Communication