Python network programming-socket simple Communication

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.