Author Topic: Run python script from POST command?! Raspberry PI  (Read 1212 times)

0 Members and 1 Guest are viewing this topic.

Offline HiSmartAlarmsTopic starter

  • Newbie
  • Posts: 6
  • Country: us
  • I love electricity.
    • YouTube Channel
Run python script from POST command?! Raspberry PI
« on: July 02, 2020, 03:33:47 am »
So, I had an idea to convert a project of mine from arduino to raspberry pi. The arduino version had a software controlling it, I wanted to do the same, but want it to be controlled via POST web commands (so that way it's not limited to my USB cable length). Is there anyway to make a python script run from when nginx recieves a POST command? (note i may not know what im talking about lmao)
I love electricity. It tickles.
 

Offline greenpossum

  • Frequent Contributor
  • **
  • Posts: 408
  • Country: au
Re: Run python script from POST command?! Raspberry PI
« Reply #1 on: July 03, 2020, 10:53:20 pm »
Is there a reason you want to run nginx as the web server? Search results show that running CGI scripts from nginx is more convoluted than if you run some other webserver like apache or lighttpd.
 

Offline GeorgeOfTheJungle

  • Super Contributor
  • ***
  • !
  • Posts: 2699
  • Country: tr
Re: Run python script from POST command?! Raspberry PI
« Reply #2 on: July 05, 2020, 01:25:43 am »
1) Configure in nginx the path ("location") to proxy_pass to node-red and, 2) In node-red use the http request/response nodes and (say) node-red-contrib-pythonshell.

Easy peasy, you can have it running in a sec.
« Last Edit: July 05, 2020, 07:25:53 am by GeorgeOfTheJungle »
The further a society drifts from truth, the more it will hate those who speak it.
 

Online Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6262
  • Country: fi
    • My home page and email address
Re: Run python script from POST command?! Raspberry PI
« Reply #3 on: July 05, 2020, 03:20:59 am »
(I removed my post, because this is the wrong forum to talk about how to do IOT securely.)
 

Offline Chris42

  • Contributor
  • Posts: 32
  • Country: pl
Re: Run python script from POST command?! Raspberry PI
« Reply #4 on: July 08, 2020, 09:59:22 pm »
I think the best option for you is to run a simple app in flask. It is a framework for creating server side software.

Here is a quick hello world for POST request:

Code: [Select]
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/enableSkynet', methods=['POST'])
def enableSkynet():
    data = request.json
    # here is your parameter
    parameter = request.json["parameterName"]
    print(parameter)

As a body of the post request you pass a JSON encoded object containing whatever parameters you need.
Here is how you write JSON: https://www.tutorialspoint.com/json/json_syntax.htm

To run the above code you need to install the following dependencies (available via pip):
 - flask
 - flask-jsonpify
 - flask-restful

Flask has a built in debug server that you can run by simply running your app script from a console.

Once you are ready to deploy you can run flask on NGINX. Here is a link to a tutorial that demonstrates just that: https://www.raspberrypi-spy.co.uk/2018/12/running-flask-under-nginx-raspberry-p
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf