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