Python development [Article 8]: socket network programming, python Article 8

Source: Internet
Author: User

Python development [Article 8]: socket network programming, python Article 8

Server:

Client:

Server output:

Client output:

Continuous sending information:

Server:

Client:

Run commands on the linux Client

Server:

Client:

File Sending in linux

Server:

Client:

Up to 32768 data records can be sent at a time (different systems)

Send multiple times

Server:

Client:

File changes for each interaction:

Socket implements simple ssh

Server:

Client:

Pay attention to the socket package sticking problem.

Method 1: Add sleep between multiple connected send messages.

Method 2: perform an interaction after each send.

Server:

Client:

Socket implements simple ftp

Ftp server:

1. Read the file name

2. Check whether the file exists

3. Open a file

4. Check the file size

5. Send the file size to the client

6. wait for confirmation from the client

7. Start reading and sending data

8. Send md5

Server:

Client:

SocketServer

Implement concurrent processing.

The socketserver module simplifies the task of writing network servers.

Common socketserver types

Class socketserver. TCPServer (server_address, RequestHandlerClass, bind_and_activate = True)

This uses the Internet TCP protocol, which provides for continuous streams of data between the client and server.

Class socketserver. UDPServer (server_address, RequestHandlerClass, bind_and_activate = True)

This uses resume rams, which are discrete packets of information that may arrive out of order or be lost while in transit. The parameters are the same as for TCPServer.

Create a socketserver in at least a few steps:

1. You mast create a request handler class by subclassing the BaseRequestHandler class and overriding its handle () method; this method will process incoming requests.

Create a request processing class that inherits BaseRequestHandler and overwrites the parent class handle ().

2. You mast instantiate one of the server classes, passing it the server's address and the request handler class.

Instantiate the TCPServer and pass the server ip address and the request processing class created above to the TCPServer.

3. Then call the handle_request () or server_forever () method of the server object to process one or multiple requests.

Server. handle_request (): Only one request is processed.

Server. server_forever (): process multiple requests

4. Call server_close () to close the socket.

Server:

Client:

Start a new thread

 

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.