I am starting a project on my raspberry pi in which I am trying to control a servo hat (pimoroni pan tilt) from a web server.

How can I host a Lan website from the pi, then control parts of a python script from buttons on that website?

3 Answers 3


There are many approaches. Guess the simplest is to use a HTTP form that send to a cgi (in your case directly a python script) You need to

  • install a web server (apache, lighttpd, nginx,...)
  • install mod_cgi or fastcgi
  • configure the web server to run python
  • run a web site that send a HTTP form to the python script
  • the python script can control your servo

there are many options... keep it simple in the beginning.


I know it is not a correct way to answer. You can consider it as just a comment.(since I can't make comment without 50 reputation I adding this as answer). For this work I will suggest you Flask server, or bottle server. Controlling Python program using Apache server is little bit difficult. But the Bottle and Flask server uses Python web framework itself. I believes this article will help you alot. If you want to use apache2 and php google about shell_exec or exec function of php. I believes this question is already asked on Stack exchange. Now I will show you an example to turning on and off light using(controlling gpio pins) using bottle server and know the status of switch. The switch is connected to gpio 17 and led is connected to gpio 18. We also will create a button using html to control LED.

from bottle import route, run import RPi.GPIO as GPIO
host = 'IP address of RasPi' 
pinButton = 17 
pinLed = 18
GPIO.setmode(GPIO.BCM) # pin 17 as input with pull-up resistor

GPIO.setup(pinButton, GPIO.IN, pull_up_down=GPIO.PUD_UP) # pin 18 as output
GPIO.setup(pinLed, GPIO.OUT) LedState = False
def read_button(): 
    if GPIO.input(pinButton): 
        return 'Released' 
        return 'Pressed'
def update_led():
    GPIO.output(pinLed, LedState) def toggleLed():
    global LedState 
    LedState = not LedState
def index(arg=""): 
if arg == "toggle": 
    update_led() response = "<html>\n" 
    response += "<body>\n"
    response += "<script>\n" 
    response += "function changed()\n" 
    response += "{window.location.href='/toggle'}\n"
    response += "</script>\n"
    response += "Button: " + read_button() +"\n" 
    response += "<br/><br/>\n"
    response += "<input type='button' onClick='changed()' value=' LED '/>\n" 
    response += "</body></html>\n"
    return response
run(host=host, port=80)
  • 1
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – Ghanima
    Jun 20, 2017 at 17:33
  • @Ghanima Hai Ghanima I included links for reference about flask server. You may not observe that. The flask and bottle server are python powered servers. That is why we there is no necessity to run Python script from buttons of the website(since server running on python) as he asked in the question. I believes so Raspberry Pi foundation is also promotes Flask server( raspberrypi.org/learning/python-web-server-with-flask/worksheet ). I don't get what is the reason to down vote me...
    – Arun
    Jun 22, 2017 at 7:44
  • You shouldn't be that fast blaming others as I did in fact not down vote you.
    – Ghanima
    Jun 22, 2017 at 16:05
  • @Ghanima Sorry Ghanima, if I became rough. I am new in stack exchange. It is not easy for me to forget about a social media culture while using stack exchange. Sorry for taking your criticism as an attack.
    – Arun
    Jun 22, 2017 at 17:57
  • 1
    No harm done. Welcome to the site.
    – Ghanima
    Jun 22, 2017 at 18:08

You could use something like remot3.it (new weaved "face") to be able to connect to your RPi outside your local network. You can create commands associated with a job, or connect via ssh and run your script.

  • 1
    The OP asked about a LAN webserver, while this may work, it also opens an unnecessary security hole. Jun 20, 2017 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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