Nicolas Formichella
Nicolas Formichella

Reputation: 127

Any way to run an internal python script from a webpage?

I finally made a project that I wanted to make since a long time :

I'm using an Arduino Uno to replace my PC power button (with a simple relay) and that Arduino Board is connected to a Raspi 3 for network connection purposes

My wish is to do a webpage (or a API-Like request) that at a touch of a button (preferably in a password-protected page) It'll power the PC on

I know how to code in Python, and my script to control the Arduino is already done but I can't find a way to run, only server-side, a Python Script from a button in a webpage

I found that CherryPy framework but I don't think it'll suit my needs

Can someone give me any ideas about that please?

Upvotes: 0

Views: 72

Answers (1)

emmunaf
emmunaf

Reputation: 426

As already mentioned by @ForceBru, you need a python webserver.

If this can be useful to you, this is a possible unsecure implementation using flask:

from flask import Flask
from flask import request

app = Flask(__name__)

@app.route('/turnOn')
def hello_world():
    k = request.args.get('key')
    if k == "superSecretKey":
        # Do something ..
        return 'Ok'
    else:
        return 'Nope'

If you put this in an app.py name file and, after having installed flask (pip install flask), you run flask run you should be able to see Ok if visiting the url http://localhost:5000/turnOn?key=superSecretKey .

You could write a brief html gui with a button and a key field in a form but I leaves that to you (you need to have fun too!). To avoid potential security issues you could use a POST method and https. Look at the flask documentation for more infos.

Upvotes: 1

Related Questions