The socket in Python

Source: Internet
Author: User

In Python, the socket module is used to encapsulate the various underlying communications, support the TCP/UDP protocol, and facilitate the programming of the C/S class.

The most common usage:

such as Ftp_server:

1 ImportSocketserver2 classMysocketserver (socketserver. Baserequesthandler):3     defhandle (self):4          whileTrue:5             Try:6SELF.DATA=SELF.REQUEST.RECV (1024). Strip ()7                 Print(Self.client_address[0],'recv>>', Self.data)8 Self.request.send (Self.data.upper ())9             exceptException as E:Ten                 Print("the client is down! ", E) One                  Break AIpaddr='localhost' -port=9999 - #Server=socketserver. TCPServer ((ipaddr,port), mysocketserver) theServer=Socketserver. Threadingtcpserver ((ipaddr,port), mysocketserver) - Server.serve_forever () -Server.close_request ()
Ftp_server_code

Ftp_client

1 ImportSocket2 3Client =Socket.socket ()4Client.connect (('localhost', 9999))5  whileTrue:6data = input (">>>>>>")7     ifLen (data) = = 0:Continue8Client.send (Data.encode ("Utf-8"))9Re_data = CLIENT.RECV (1024)Ten     Print(Re_data) OneClient.close ()
Ftp_client_code

The socket in Python

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.