1

I'm helping to build a Django app that will interface with a voip phone system, and they've created some scripts to execute when an action is performed (add, update,delete) in order to have parity with the phone system side. They've asked me to pass the parameters to the script like this /var/www/html/om/om_add.sh deviceid filename title They've also mentioned to pass the title as a url, using urllib.quote_plus(title) Lastly, it should occur after each action is executed in the app. Now, I've heard that executing shell scripts from a Django app is not advised, but wondering if this is a different case? Also, how I can go about doing this, the only way I know how to pass those params is to pass them through a view. Any help is greatly appreciated!

def post_create(request):
 device = request.session['device']
 if device == 'dummy':
 return render(request,'access_denied.html')
 did = {'device_id': device }
 form= PostForm(request.POST or None, request.FILES or None,initial=did)
 if form.is_valid():
 instance = form.save(commit=False)
 check = Sounds.objects.all().filter(device_id= device).filter(target=instance.target).exclude(target='generic').first()
 if check:
 instance.pk = check.pk
 instance.device_id= device
 instance.save() 
 return HttpResponseRedirect('/')
 else:
 instance.device_id = device
 instance.save()
 dev = instance.device_id
 name = instance.sound.name
 title = urllib.quote_plus(instance.title) 
 os.execv('/var/www/html/ogm/ogm_add.sh', [dev,name,title])
 return HttpResponseRedirect('/')
 context= {
 'form': form,
 }
 return render(request, 'post_form.html',context,)
asked Dec 6, 2017 at 22:04

1 Answer 1

2

executing a shell script has nothing to do with django framework it's core python future .

import os
os.execv('path' , ['arg'])

the arg can be a list or a tuple you can pass it from database or url etc.

the folowing simple is match batter for this purpose

from subprocess import call 
sub = call(['ls' , '-l'])
print(sub)

the sub contain the output

answered Dec 6, 2017 at 22:29
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer! So the way I'm trying to implement it may be stupid, but can you possibly check out the code I've added above (within the create view) , specifically after the "else" statement. It seems to work correctly on my end but I'm not sure on how to check if its actually working and running the script

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.