title: How to execute python script using CherryPy framework
body: <p>I've been working on side project to learn python coding better.
  Since I reciantly started college I've been eager to learn more abut the language that we work with.</p>
  
  <p>So basicly I am using Raspberry Pi to work with in school and out.
  Reciantly I had an idea by using ultrasound sensor to detect the distance of object(in this case hand) to light up setted up LED in regards of the objects distance. Basicly I got the code working and seperatly it does work. But I'm here for a different reason.</p>
  
  <p>I want to implament it to the CherryPy webserver, that I could activate the sensor to start it's work, afterwards deactivate by pressing other button in web interface. Everything needs to be controlled in web interface with html butons.</p>
  
  <p>Any ideas or anything feell free to post them.
  Any more questions please ask.</p>
  
  <pre><code>import cherrypy
  #import blink
  import RPi.GPIO as GPIO
  import time
  import datetime
  
  from subprocess import check_output
  ipaddr = check_output(['hostname','-I']).strip().decode('utf-8')
  
  file = open('/home/pi/htmlfile.html')
  index_html = file.read()
  file.close()
  index_html = index_html.format(IP=ipaddr)
  
  class HelloWorld(object):
  @cherrypy.expose
  def index(self):
      return index_html
  
  @cherrypy.expose
  def goat(self):
      return "There is no goats here!"
  
  @cherrypy.expose
  def blink(self):
      #os.system("blink.py 1")
      GPIO.setwarnings(False)
      GPIO.setmode(GPIO.BCM)
      GPIO.setup(4, GPIO.OUT)
      #GPIO.setup(17, GPIO.OUT)
      #GPIO.setup(27, GPIO.OUT)
      i = 0
      while (i&lt;8):
          GPIO.output(4, GPIO.HIGH)
          #GPIO.output(17, GPIO.HIGH)
          #GPIO.output(27, GPIO.HIGH)
          time.sleep(0.5)
          GPIO.output(4, GPIO.LOW)
          #GPIO.output(17, GPIO.LOW)
          #GPIO.output(27, GPIO.LOW)
          time.sleep(0.5)
          i = i+1
      return "Blinking finished"
  
  @cherrypy.expose
  def yellowon(self):
      GPIO.output(4, GPIO.HIGH)
      return index_html
  
  @cherrypy.expose
  def off(self):
      GPIO.output(4, GPIO.LOW)
      GPIO.output(17, GPIO.LOW)
      GPIO.output(27, GPIO.LOW)
      return index_html
  
  @cherrypy.expose
  def greenon(self):
      GPIO.output(17, GPIO.HIGH)
      return index_html
  
  @cherrypy.expose
  def redon(self):
      GPIO.output(27, GPIO.HIGH)
      return index_html
  
  @cherrypy.expose
  def sensoron(self):
      def reading(sensor):
          pingtime = 0
          echotime = 0
  
          if sensor == 0:
              GPIO.output(21,GPIO.LOW)
  
          if(GPIO.input(18)==1):
              print("HIGH")
          else:
              print("LOW")
  
              GPIO.output(21,GPIO.HIGH)
              pingtime=time.time()
              time.sleep(0.00001)
              GPIO.output(21,GPIO.LOW)
  
              while GPIO.input(16)==0:
                  pingtime = time.time()
  
              while GPIO.input(16)==1:
                  echotime=time.time()
  
              if (echotime is not None) and (pingtime is not None):
                  elapsedtime = echotime - pingtime
                  distance = elapsedtime * 17000
              else:
                  distance = 0
  
                  #print(pingtime)
                  #print(echotime)
  
              if distance &lt; 10:
                  GPIO.output(4,GPIO.HIGH)
                  GPIO.output(17,GPIO.LOW)
                  GPIO.output(27,GPIO.LOW)
              #print("AMBER")
              elif distance &lt; 20 and distance &gt;= 10:
                  GPIO.output(4,GPIO.LOW)
                  GPIO.output(17,GPIO.HIGH)
                  GPIO.output(27,GPIO.LOW)
              #print("GREEN")
              elif distance &gt;= 20:
                  GPIO.output(4,GPIO.LOW)
                  GPIO.output(17,GPIO.LOW)
                  GPIO.output(27,GPIO.HIGH)
              #print("RED") 
  
              return distance
      return index_html
      file = open('/home/pi/new.py')
      return index_html
  
  @cherrypy.expose
  def sensoroff(self):
      def reading(sensor):
          pingtime = 0
          echotime = 0
          GPIO.output(4, GPIO.LOW)
          GPIO.output(17, GPIO.LOW)
          GPIO.output(27, GPIO. LOW)
      file.close()
      return index_html
  
  if __name__ == '__main__':
      GPIO.setwarnings(False)
      GPIO.setmode(GPIO.BCM)
      GPIO.setup(21, GPIO.OUT)
      GPIO.setup(16, GPIO.IN)
      GPIO.setup(4, GPIO.OUT)
      GPIO.setup(17, GPIO.OUT)
      GPIO.setup(27, GPIO.OUT)
      cherrypy.server.socket_host = ipaddr
      cherrypy.quickstart(HelloWorld())
  </code></pre>
  
  <p>Thank you for help in advance :)</p>