I am trying to run python script in Apache 2.x with mod_python. I edited httpd.conf with publisher

     LoadModule python_module /usr/local/apache2/modules/mod_python.so
     <Directory /usr/local/apache2/htdocs/mod_python>

    SetHandler mod_python
    PythonHandler mod_python.publisher
    PythonDebug On

I am trying to add a rule in firewall using python script which require root privilege. it's asking for root privilege ? Please somebody help.


You need to use sudo to run pfctl from your web application and you need to add the "apache" user to the sudoers file with the rights to run pfctl without asking for a password.

NOTE: To modify your sudoers file use the command visudo as root, or an account with elevated privileges to do so.

You might also not want to use the obsolete mod_python way of running python web apps. Something like Bottle or Flask paired with the nginx in base would be a better option.

| improve this answer | |
  • I am not using pfctl through python.I am using py-pf for adding rule in firewall(pf).HOw can i execute python script require root privilage. one more constraint from GUI ... exec in PHP is not allowed.. :( – Uday Pratap Singh Apr 10 '13 at 6:14
  • Then the python script using py-pf must be standalone from the web app and run with sudo. It's that or running apache as root, which I strongly recommend against. – Lars Hansson Apr 12 '13 at 9:15

Check it out.

"Similarly to pfctl(8), py-pf sends commands to the kernel through the ioctl(2) interface provided by the pf(4) pseudo-device; this allows Python to natively communicate with the kernel, thanks to the fcntl and ctypes modules, with no need to write a specific C extension module."

So, check permissions in pf pseudo-device by user running apache.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.