Search code examples
pythonpopenpython-os

Replace os.system with os.popen for security purposes


Is it possible to use os.popen() to achieve a result similar to os.system? I know that os.popen() is more secure, but I want to know how to be able to actually run the commands through this function. When using os.system(), things can get very insecure and I want to be able to have a secure way of accessing terminal commands.


Solution

  • Anything that uses the shell to execute commands is insecure for obvious reasons (you don't want someone running rm -rf / in your shell :). Both os.system and os.popen use the shell.

    For security, use the subprocess module with shell = False

    Either way, both of those functions have been deprecated since Python 2.6