6

I want to block users for using the command prompt, but I also want to allow them to use Python. However, Python has the os module which users can use to execute commands, like:

>>> import os
>>> os.popen("command to be run eg start "C:/")

How can I stop Python from allowing os.popen (above) to run?

techraf
  • 9,159
  • 11
  • 45
  • 63
AlexanderRD
  • 163
  • 4

2 Answers2

5

You should be looking for a full sandbox, and not just preventing os from being imported.

For instance, you can use RestrictedPython for blocking imports.

Or you can set sys.modules['os']=None. Look at this related post for more options.

Jedi
  • 3,976
  • 2
  • 26
  • 42
4

Instead of blocking, you could allow users to run python in a "jail". They would be able to run commands, but in a safe sandbox.

CristianTM
  • 2,580
  • 19
  • 21