0

Possible Duplicate:
Calling an external command in Python

I would like to call various programs from my Python script, like binary programs, but also other perl/python/ruby scripts, like wget, sqlmap and custom scripts.

The problem is that I would like the user to be able to change parameters of the underlying program. Let's take wget for example. Let's say I'm calling this program (note that all three parameters are dynamically inputted into the command):

wget www.google.com --user=user --password=pass

But I would also like the user to add custom parameters to the wget command. I guess the best way would be directly from a file, but I was wondering if something like this exists so that I won't reprogram everything by hand.

Also keep in mind that this is not just 1 program, but it could be up to 100 programs, maybe more. It needs to be extendable and not too complicated for the user to change.

Thanks

Community
  • 1
  • 1
eleanor
  • 1,484
  • 3
  • 19
  • 35

1 Answers1

0

One quick example using subprocess.check_output

program = 'wget'
default_args = ['www.google.com']
user_args = []
subprocess.check_output(program + default_args + user_args)

Just be very carefull with this, do all the security checks before allowing any user to add parameters to a command.

You may also need shlex.split to split the user supplied arguments before adding them to subprocess call

If you want to have the defaults in external files you could do something like this:

with open('wget_defaults.txt') as i:
    default_args = i.read().split(',')

Hope it helps

Facundo Casco
  • 8,729
  • 5
  • 40
  • 61
  • Hi, I'm not asking how to do it, clearly I do know how to do that, it's pretty simple. I'm asking how to do it so I can extend it pretty easy. I would like to have the parameters in .txt for various programs: there's not a very hard way to do this; I'm only asking if something like that exists, so I can use it. Please correct your answer. – eleanor Jan 19 '13 at 00:22