Python way to implement Scoket communication

Source: Internet
Author: User

To understand this network communication is really inseparable from the implementation of the flowchart, see the general many know, think this Bo master painting is good, the address is

Http://www.cnblogs.com/wangcq/p/3520400.html

1. Understand which layer it is in the transmission

2. Understanding its implementation flowchart

So, we can summarize his way of implementation and logic, the code is as follows

Server-side request 1. Create object 2. Bind the socket to the specified address, socket.bing (address) 3.3rd, after binding, the socket must be prepared, and the port is monitored Socket.listen 4. The Accept method of the socket waits for a client to request a connection 5. Processing Phase 6. The server calls the Close method of the socket to close the connection # server  import socket  address = (' 127.0.0.1 ', 31500)  s = socket.socket (socket.af_inet, socket. SOCK_STREAM) # s = Socket.socket ()  s.bind (address)  S.listen (5)  ss, addr = s.accept ()  print ' This is Server ', addr  ss.send (' server ')  RA = ss.recv (+)  print ra  ss.close ()  s.close ()  

  

Client creation steps: 1. Create a socket connection server 2. Connect to Server Socket.connect ((host,port)) 3 using the Connect method of the socket. Customers and servers communicate via send and Recv methods 4. Call the Close method of the socket to communicate import socket    address = (' 127.0.0.1 ', 31500)  s = Socket.socket ( Socket.af_inet, Socket. SOCK_STREAM)  s.connect (address)    data = S.RECV (+)  print ' This is client ', Data    s.send (' client ')    S.close ()

Communication results

 

Python way to implement Scoket communication

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.