Advanced Language Call System command

Source: Internet
Author: User

Advanced languages can also invoke system commands, sometimes with unexpected output language types python php c 1. There are three common methods of executing system commands in Python python: Os.system os.popen Using Module subprocess

The former return value is the script's exit status code, both of which require an OS module (import OS) (1) Os.system

When the command is executed, the running state is displayed in the window, and the return value is the status code

Import OS
a=os.system (' ping ')
print a
A for the return value 0 represents the success 1 represents the small problem 2 represents the big problem (2) Os.popen

Its return value is a file object, and you can perform related operations on this file object.

Import OS
a=os.popen (' ping ')
print a.read ()

A is a file object, you can call the object's Read method and the Close method for related operations (3) using module subprocess (child processes)

Import subprocess
a=subprocess. Popen (' ping ', shell=true)
The shell is set to true and the program is executed through the shell. Popen.poll (): Used to check whether the child process has ended. Sets and returns the ReturnCode property. Popen.wait (): Wait for child process to end. Sets and returns the ReturnCode property. 2. PHPShell_exec exec (1) shell_exec

Gets all output values that the command performs

<?php
$f =shell_exec (' Ipconfig/all ');
echo $f;
? >      

Output All information (2) EXEC

Gets the output value of the last command

<?php
$f =exec (' Ipconfig/all ');
echo $f;
? >      

Only the last row of 3 is output . C

Header file for include "Stdlib.h"
int main ()
{
System ("ping");
}

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.