title: Sense HAT CGI script
body: <p>I am trying to read various values from a Sense HAT by creating a CGI script that measures the values and returns them in the form of a JSON document.</p>
  <p>This is the code running in the script.</p>
  <pre><code>#!/usr/bin/python3
  
  # Script to read sensor data from the sense hat and return it as a JSON document
  
  import sense_hat
  import json
  import cgitb
  
  cgitb.enable()
  
  # Set up sensehat.
  sense = sense_hat.SenseHat()
  
  # Calling these functions because the get functions will sometimes return zero when used for the first time.
  sense._init_humidity()
  sense._init_pressure()
  
  # Set up json document
  output = {
      &quot;humidity&quot;: sense.get_humidity(),
      &quot;pressure&quot;: sense.get_pressure(), 
      &quot;temperature&quot; : sense.get_temperature()
  }
  
  # Encode json to string and return to STDOUT
  print(&quot;Content-Type: application/json\n&quot;)
  print(json.dumps(output))
  </code></pre>
  <p>I have set up the apache2 webserver to accept .py files in the <code>/var/www/cgi-bin</code> folder. A simple hello world has verified that this is working.</p>
  <p>When attempting to access the script through a browser, an Internal Server Error is reported. I've manually executed the script as <code>www-data</code> (the user that runs cgi-scripts) and I think that user doesn't have enough permissions to use the sense hat.</p>
  <pre><code>Traceback (most recent call last):
    File &quot;./throw.py&quot;, line 12, in &lt;module&gt;
      sense = sense_hat.SenseHat()
    File &quot;/usr/lib/python3/dist-packages/sense_hat/sense_hat.py&quot;, line 92, in __init__
      self._stick = SenseStick()
    File &quot;/usr/lib/python3/dist-packages/sense_hat/stick.py&quot;, line 57, in __init__
      self._stick_file = io.open(self._stick_device(), 'rb', buffering=0)
  PermissionError: [Errno 13] Permission denied: '/dev/input/event0'
  </code></pre>
  <p>How can I best go about this?</p>