Recently I stumble upon Envoy. Envoy is a wrapper around the subprocess module and
is supposed to humanize subprocess of Python.
Its written by Kenneth Reitz (the author of “Requests: HTTP for Humans“)
It was written to be an easy to use alternative to subprocess.
“Envoy: Python Subprocesses for Humans.”
Envoy is available from PyPI and can be installed with pip.
Search for the Envoy package via the pip command-line tool.
" envoy - Simple API for running external processes. "
Install Envoy
Just like with any other Python modules, we have to import them first.
Start your Python interpreter and type “import envoy”
Great, Envoy is imported, now we can start to discover its functions etc.
After you have imported a module in Python, it’s always good to see what functions
,classes and methods that the module provides. One way of doing that is using
“dir(envoy)”.
That will list the names of all functions and variables, that are defined in the
Envoy module.
That will give you an output like this:
1 2 3 |
|
If you want to get one name per line, just run a simple for loop:
1 |
|
This output shows you one name per line:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
You can also use “help(envoy)” to get the documentation on all the functions.
Let’s take a look at the “run” method for Envoy.
To check the uptime of our machine, we can use the “uptime” command.
To see the output, we add “std_out”:
1 2 |
|
To get the status code, add “status_code” to your object.
Run a command, get the response:
1 2 |
|
To get the standard error, add “std_err”.
Get history.
1 2 |
|
Check for the Chrome process
1 2 |
|
In our last example, I make use of multiple commands.
1 2 3 4 5 6 7 |
|
Get the status code of all commands
1 2 3 4 5 6 7 |
|
Get the status code + the output of each command
1 2 3 4 5 6 7 |
|
Envoy has become my main library to handle external command calls.
It was written to be an easy to use alternative to subprocess and the convenience
of envoy.run is really great.
::...
或是邮件反馈可也:
askdama[AT]googlegroups.com
订阅 substack 体验古早写作:
关注公众号, 持续获得相关各种嗯哼: