The path to learning in Python--socket

Source: Internet
Author: User

Sockets are also commonly referred to as "sockets," which describe IP addresses and ports, and are a handle to a communication chain that can be used to communicate between different virtual machines or different computers.

Socket Server Example:

ImportSocketip_port= ('127.0.0.1', 9999)#set the IP address and port on the server side. SK= Socket.socket ()#call the socket constructor. Sk.bind (Ip_port)#bind the server-side IP and port using the Bind method. It can also be understood to register IP and port information with the system. Sk.listen (5)#sets the maximum number of connections.  whileTrue:Print('Server Waiting ...') conn,addr= Sk.accept ()#the server socket waits for the client to request a connection through the socket's accept method. Client_data = CONN.RECV (1024)#set up to accept up to 1024 bytes at a time    Print(Str (Client_data,'UTF8'))#The print service side receives the client's information. Conn.sendall (Bytes ('Don't answer, don't answer , don't answer','UTF8'))#use the Sendall method to send messages to the client. Conn.close ()#Close the connection. 
Socket Server Example:

Socket Client Example:

ImportSocketip_port= ('127.0.0.1', 9999)#set the IP and open ports on the server side. SK= Socket.socket ()#calling the socket constructorSk.connect (Ip_port)#connect to the server using the socket's Connect methodsk.sendall (Bytes ('request to occupy the Earth','UTF8'))#use the Sendall method to send messages to the server. server_reply = SK.RECV (1024)#set up to accept up to 1024 bytes at a timePrint(Str (server_reply,'UTF8'))#Print out the received service-side back information. Sk.close ()#close the socket connection. 
Socket Client Example:

The path to learning in Python--socket

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.