Python Network programming

Source: Internet
Author: User

Tag: Return using ROM Code connect GPO ODI end--

Introduction: Network programming is mainly divided into UDP and TCP two, UDP does not need to create a connection, so the transfer speed, but not robust, TCP needs to create a connection, so the transmission speed is slow, but robust, now, most applications use TCP to communicate.

A, UDP

1.send Terminal

1) Guide Package from socket import *

2) Create Udpsocket object, Udpsocket = socket (af_inet,sock_dgream)

3) Call the object to send the message, Udpsocket.sendto ("hello,udp, I Came". Encode ("Utf-8"), ("The IP address of the machine you want to send", the port)

4) Close Udpsocket object, Udpsocket.close ()

2.recive Terminal

1) Guide Package from socket import *

2) Create Udpsocket object, Udpsocket = socket (af_inet,sock_dgream)

3) Bind IP and port, Udpsocket.bind (("IP address of the machine you want to receive", port)

4) Receive data, Udpsocket.recvfrom (1024)

5) Close Udpsocket object, Udpsocket.close ()

  
1  fromSocket Import *2 3Udpsocket =socket (Af_inet,sock_dgram)4 #使用udp发送的数据, each time the need to write on the receiver's IP and port, you can tell the receiver in what way to encode5Udpsocket.sendto ("haha\n". Encode ("gb2312"),("172.25.6.222",8080))6 7Udpsocket.sendto ("Oh, hello.". Encode ("Utf-8"),("202.193.52.52",1880))
Send End   
1 # coding = Utf-8 2 3  from Import *4 udpsocket = socket (af_inet,sock_dgram)56 udpsocket.bind (( ", 8080))7 recvdata = Udpsocket.recvfrom (1024x768)8Print (Recvdata[0].decode ("utf-8"))
recive End

Second, TCP

1.server Terminal

1) Guide Package from socket import *

2) Create Tcpsocket object, Tcpsocket = socket (af_inet,sock_stream)

3) Bind IP and port, tcpsocket.bind ("Server-side IP", port)

4) Monitoring, Tcpsocket.listen (5)

5) Receive client side, Clientsocket,clientinfo = Tcpsocket.accept ()

6) Print client-side request information, RecvData = CLIENTSOCKET.RECV (1024x768), print (RecvData)

2.client Terminal

1) Guide Package from socket import *

2) Create Tcpsocket object, Tcpsocket = socket (af_inet,sock_stream)

3) Connect server side, Tcpsocket.connect ("Server IP Address", port)

4) Send data to server side, Clientsocket.send ("hello,tcp, here I Come". Encode ("Utf-8"))

5) Print the information returned from server side, RecvData = Clientsocket.recv (1024x768), print (RecvData)

6) Close the client, Clientsocket.close ()

  
1  fromSocketImport*2 3ServerSocket =socket (af_inet, sock_stream)4 5Serversocket.bind (("", 8899))6 7Serversocket.listen (5)8 9 Print("-----1-----")TenClientsocket,clientinfo =serversocket.accept () One  A Print("-----2-----") - #Clientsocket indicates that the new client - #The clientinfo represents the IP of this new client and the port the  -RecvData = CLIENTSOCKET.RECV (1024) -  - Print("-----3-----") + Print("%s:%s"%(str (clientinfo), recvdata)) -  + clientsocket.close () AServersocket.close ()
Server Side   
1  fromSocketImport*2 3Clientsocket =socket (af_inet, sock_stream)4Clientsocket.connect (("192.168.119.153", 8989))5 6 #Note:7 #1. The TCP client has already linked the server, so in the future data send, do not need to fill in the other iph and port-----> Phone8 #2. UDP When sending data, because there is no previous link, according to the need to fill in each send the receiver's IP and port-----> letter9Clientsocket.send ("haha". Encode ("gb2312"))Ten  OneRecvData = CLIENTSOCKET.RECV (1024) A  - Print("recvdata:%s"%recvdata) -  theClientsocket.close ()
Client Side

Python Network programming

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.