Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    python, CGI & /dev/mem problem

    I am trying to turn on and off GPIO ports on a Raspberry Pi via CGI Python scripts. The script I have is (the python bit run as root via the command line works fine):

    #!/usr/bin/env python
    import cgi
    import cgitb; cgitb.enable()  
    import RPi.GPIO as GPIO
    GPIO.setup(11, GPIO.OUT)
    GPIO.output(11, False)
    print "Content-type: text/html"
    print """
    <head><title>light goes on</title></head>
    <h3> light is on</h3>

    But when run this via a web browser, I get the following error:
    <class 'RPi.GPIO.SetupException'> Python 2.7.3: /usr/bin/python
    Fri Feb 15 20:58:41 2013 
    A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
    /usr/lib/cgi-bin/ in () 
    4 import cgitb; cgitb.enable() # for troubleshooting
    => 6 import RPi.GPIO as GPIO
    7 GPIO.setwarnings(False)
    8 GPIO.setmode(GPIO.BOARD)
    RPi undefined, GPIO undefined 
    <class 'RPi.GPIO.SetupException'>: No access to /dev/mem. Try running as root! 
    args = ('No access to /dev/mem. Try running as root!',) 
    message = 'No access to /dev/mem. Try running as root!'
    Is there another way for this to work? I am a beginner at all this so please keep the replies simple!

    Any help would be great.
    Last edited by atreyu; 02-17-2013 at 01:34 AM. Reason: added CODE tags to aid readability

  2. #2
    Like the error says, you need root access to write to /dev/mem, and the user calling the program doing the accessing is the web user (not sure who that is, "apache" or "nobody" possibly).

    One solution would be to set up sudo to be able to run a script that does your GPIO stuff. Then your CGI script could call that script, via sudo.

    First find out who your web user is. Look for it in the output of ps, e.g.:

    ps auxww|egrep 'httpd|apache'
    The first column will contain the username.

    Then write your script that does the GPIO commands. let's say it is "/usr/local/bin/". Make sure it is executable (chmod +x you know what to put into that script. probably a good idea to make it accept arguments, so you can make it dynamic.

    Then set up sudo so that the web user can run that script. Typically you'd use "visudo". Have to run as root, though. Either "su" to root, or on some Linux distros (Ubuntu, etc.), you can use "sudo".

    So open a terminal, and, to su:
    su -
    then enter the root password. then run visudo:
    or if *Buntu:
    sudo visudo
    in either case, now you have the sudoers file (/etc/sudoers) open in an editor (probably vi). add a line something like this:
    apache ALL = (root) NOPASSWD: /usr/local/bin/
    you might also have to disable requiretty in the sudoers file. Substitute the first line (apache) with whatever is the username of the user running your web server process (gotten from the ps output).

    Then in your CGI script, call that script, instead of running GPIO commands directly, e.g.:

    sudo /usr/local/bin/
    Let us know how you get on.

  3. #3
    Thanks for the reply, that has done the job, because all the scripts are python i have had to add python to the sudoers file, not the individual scripts. I assume there are good security reasons (?) not to do this, but as this is going to be a web server only accessible from my home network, i am more than happy with this solution.

    Many thanks

  4. $spacer_open
  5. #4
    Quote Originally Posted by peted View Post
    Thanks for the reply, that has done the job, because all the scripts are python i have had to add python to the sudoers file, not the individual scripts.
    That doesn't sound right to me, but I've never tried sudo with a python script. I use sudo all the time to call bash and perl scripts (without also specifying /bin/bash or /usr/bin/perl) and it works just fine.

    purely as a learning experience, I would encourage you to find out why!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts