Python Learning Network Programming--Host command execution

Source: Internet
Author: User

Import OS
Os.system () Execute system command
Only execute commands cannot return a value
Import subprocess can execute system commands
Res=subprocess. Popen (' dir ', shell=true,stdout=subprocess. Pipe,stderr=subprocess. PIPE) The first parameter is the string form of the execution command, and the second is Shell=true, which represents the use of a command interpreter
Pipe is a feature that subprocess provides, piping. Can let the results do not print in the pipeline inside
Print (Res.stdout.read (). Decode (' GBK ')) correctly outputs the pipe with STDOUT, the data inside the read pipeline
Windows is in GBK format, so go to GBK
StdErr is the wrong result stdout is the correct result
If you want to make the user error and the correct results can be seen, to build two pipelines a correct one wrong
If the command is correct, go to the correct pipe stdout get the results.
If the command is wrong, go into the wrong pipe stderr and fetch the result.
How to use pipelines in commands:
To see which task commands are open in Windows: tasklist
If you want to see if you're running a pycharm or something: findstr pycharm
In the program: Tasklist! findstr pycharm exclamation point is the pipe
A pipe tasklist will temporarily present the results in the pipeline and then be judged by the findstr pycharm.
There is also a pipeline:
Import subprocess
Subprocess. Popen (' dir ', shell=true,stdin=subprocess. Pipe) is also a conduit

If the maximum recv is 1024 when you enter a command, such as tasklist, there are too many command data, if the fetch data is greater than 1024
Then the data in the pipeline cannot be completely removed. Then enter the data in the pipe to be taken out of the command and the command will be messy.
Sock_stream Streaming Protocol is the TCP protocol
TCP uses the Nagle algorithm, which combines small data with smaller data volumes into a single big data issue.
So there will be sticky bag phenomenon, so the above data, is because the sticky bag is not clear who is who.
The socket is recommended to use from socket import *
Sticky pack phenomenon both the sending end and the service end are sticky packets
The service side receives less the rest will stay in that sticky bag, next time continue to accept
The client sends too much and also sticks the packet.
Send data long so to put the length in front, the key point is the amount of recv, high-speed length can solve the problem of acceptance
But once came in too big, larger than the system memory can not be saved, so it is necessary to pass data side by side to write data, loop accept
The download file is the process of opening a file to pass the file data to the receiving end and then write the new file.

The header is bytes, the header is fixed and the length is determined.
The bytes to let the receiver know how much is my header is not so header fixed length.
To use a struct module
Struct.pack (' i ', 123456), in parentheses I is an integral type, and the data of the later integer is packaged into a bytes format
Fixed length is 4 so the length of the header can be fixed with this module
Struct.unpack (' I ', received) unpacking, will be packaged when the data receive understanding package out. Take it out, it's a meta-ancestor.
The first data is packaged so index 0 is taken directly

Because the struct.pack I format cannot be wrapped in a header, there's a lot of data to pass
So we're going to use a dictionary to package the header.

Python Learning Network Programming--Host command execution

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.