How can I do that? Can you guide me?
That's an extremely open-ended question that basically means "please
write my code for me", but I'll give it a go. Note that I haven't done
this before, so take what I say with a grain of salt, and I look forward
to corrections from others.
Suppose you have a function that you want to run on the inside of the
chroot. So you build a module like this:
# module inside.py
def function(a, b, c):
...
if __name__ == '__main__':
args = sys.argv[1:]
function(*args)
Make sure this module is executable: on Linux systems, you would use
chmod u+x inside.py
The next bit is the part I have no idea about... use your operating
system tools to set up a chroot jail for "inside.py". Google is your
friend there, I'm sure there will be many, many websites that discuss
chroot jails.
Finally, you have your outside script that calls the inner one:
# module outside.py
from subprocess import call
return_code = call(["path/to_jail/inside.py",
"first arg", "second arg", "third arg"])
For more complicated usage, you should read the docs for the subprocess
module. For quick and dirty uses, you can use:
import sys
sys.system('path/to_jail/inside.py "first arg" "second arg" "third arg"')
but I recommend against that except for throw-away scripts.
Corrections welcome!