• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Code Snippets / How to use Envoy

How to use Envoy

Author: PFB Staff Writer
Last Updated: August 27, 2020

About Envoy

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“)

Why use Envoy?

It was written to be an easy to use alternative to subprocess.

“Envoy: Python Subprocesses for Humans.”

Install Envoy

Envoy is available from PyPI and can be installed with pip.

Search for the Envoy package via the pip command-line tool.

pip search envoy

” envoy – Simple API for running external processes. “Install Envoy

$ pip install envoy

Import Envoy

Just like with any other Python modules, we have to import them first.

Start your Python interpreter and type “import envoy”

import envoy

Great, Envoy is imported, now we can start to discover its functions etc.

Envoy Methods and Attributes

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)”.

Using dir(module)

That will list the names of all functions and variables, that are defined in the
Envoy module.

>>> dir(envoy)

That will give you an output like this:

['Command', 'ConnectedCommand', 'Response', '__builtins__', '__doc__', '__file__',
'__name__', '__package__', '__path__', '__version__', 'connect', 'core', 'expand_args',
'os', 'run', 'shlex', 'subprocess', 'threading’]

If you want to get one name per line, just run a simple for loop:

>>> for i in dir(envoy): print i

This output shows you one name per line:

...
Command
ConnectedCommand
Response
__builtins__
__doc__
__file__
__name__
__package__
__path__
__version__
connect
core
expand_args
os
run
shlex
subprocess
threading
>>>

You can also use “help(envoy)” to get the documentation on all the functions.

Envoy Usage

Let’s take a look at the “run” method for Envoy.

envoy.run()

To check the uptime of our machine, we can use the “uptime” command.

r = envoy.run("uptime”)

Standard Output

To see the output, we add “std_out”:

>>> r.std_out
'15:11  up 6 days,  1:04, 3 users, load averages: 0.55 0.57 0.61
‘

Status Code

To get the status code, add “status_code” to your object.

print r.status_code
0

Command

Run a command, get the response:

>>> print r

Standard Error

To get the standard error, add “std_err”.

r.std_err

History

Get history.

r.history
[<response 'uptime'="">]

Envoy Examples

Check for the Chrome process

r = envoy.run("ps aux |grep Chrome")
print r.std_out

In our last example, I make use of multiple commands.

import envoy

cmd = ['date', "uptime", "w"]

r = envoy.run(cmd)

print r.std_out

Get the status code of all commands

import envoy

cmd = ['date', "uptime", "w"]

for command in cmd:
    r = envoy.run(cmd)
    print r.status_code

Get the status code + the output of each command

import envoy

cmd = ['date', "uptime", "w"]

for command in cmd:
    r = envoy.run(cmd)
    print r.status_code, r.std_out

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.

More Reading

https://github.com/kennethreitz/envoy
http://stackoverflow.com/search?q=envoy

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Code Snippets, envoy Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us