开发者

Restarting Apache from Django

开发者 https://www.devze.com 2023-03-17 03:38 出处:网络
I need my Django app to be able to restart the Apache server running i开发者_如何转开发t. I\'m using mod_python.

I need my Django app to be able to restart the Apache server running i开发者_如何转开发t. I'm using mod_python.

Which is the easiest way?


The only way this would be possible is if you gave your Django application root permissions, which is horribly unsafe. But, assuming that your app is running as root, you can run

import subprocess

subprocess.Popen(['/etc/init.d/apache2', 'restart'])

Or whatever command your distribution has for restarting Apache. If the Django app is not root, you may be able to run it with sudo using the pexpect python library.

import pexpect

child = pexpect.spawn('sudo /etc/init.d/apache2 restart')
child.expect('[sudo] password for .*:')
child.sendline(password)
child.interact()

Note that this way requires you to give the apache user the ability to run sudo which is also insecure. In general, I can't see a secure way to accomplish this.

0

精彩评论

暂无评论...
验证码 换一张
取 消