Simple script running on server and app communicating with it - python

new to web development and i need some help to figure out the basics.I have a website right now,which is working fine,on a VPS with Ubuntu 16.04 and Apache.Say i would like a converter in my site or in a mobile application and have a python script in my server doing all the work.How can i send the python program the request and how can the page/application receive the data back(like download link,or info like JSON).
A simple information would suffice,i don't need a thorough explanation,mostly names like protocols,etc.. maybe some sample code for Hello World.
I should add that my website runs with Wordpress and i am not looking to change that yet.I want to create a web app within wordpress,with the app written in Python.

I would suggest using something like AWS Lambda. Lambda is an AWS product that will allow you to host your function online and you can just call it with HTTP requests and GET a response. More at aws.amazon.com/lambda

You could use something like Django or Flask. Both are used for building back end of web apps.

Related

how can i send some data from php to python

I have a running python application that needs to receive some data and process them. and I also have a PHP server that can get these data. I want to send JSON data from PHP to my python app.
anyway except running a python web server and send data to it, or insert into DB and get from DB with python?
thanks.
I tried using python cherryPy web server.
#Niklas D It would be easier to answer your question, if you can give some more context about the application or use case you want to solve.
Some further possibilities are:
Glue Code (I never did it with python and php only C++ with python, but you should be able to find examples on the internet e.g. https://wiki.python.org/moin/IntegratingPythonWithOtherLanguages#PHP )
Messaging Systems like RabbitMQ, ActiveMQ, ZeroMQ, etc.
Redis (I know you said except writing to a database, but Redis provides some features for publish subscribe https://redis.io/commands/pubsub which allows you to write to Redis from the one side and get data on the other side without polling the db all the time, which is the issue you have with using a database I guess) It's a bit easier to setup and use, than a messaging system.
TCP connection between the python and php application. https://medium.com/swlh/lets-write-a-chat-app-in-python-f6783a9ac170
If you want to send data to a python application using web protocols, i.e send POST, GET requests etc then you need to create a python web app to receive and handle those requests. Which in turn needs to be running off a webserver or you could build serverless functions to handle this, see https://serverless.com/
If you want to get data using a python application, i.e the python app sends POST and GET requests etc to your php app to ask for the JSON payload you can build an app using python's standard requests library https://docs.python.org/3/library/urllib.request.html or better still us the Requests package http://docs.python-requests.org/en/master/
Or you could do something and save the JSON file to disk and then open it with your python app. You'd need to set up scheduling or make your php app execute python code on the server... This last suggestion is a bad idea please don't unless your app is isolated and not publicly accessible or you know how to lock down your security.

Run a Python script from a HTML page using nginx on a Raspberry Pi

I've been working on a project recently where basically I need to make a motor spin at certain times throughout the day for a few seconds, that can be customised using your phone.
So far I have followed many tutorials and done a lot of browsing and I've managed to have my Pi zero host its own network(using nginx, hostapd and dnsmasq), which you can connect to on your phone and go to 192.168.4.1 to access an index.html page in /var/www/html/
I also have a Python script which, when run, turns one of the GPIO pins on for a few seconds and then off again, and this GPIO pin is in turn connected to the motor.
The trouble I am having is setting up the rest of the web side, where you are able to connect to the network, go to a page, insert 2 or 3 different times, submit them, and then when it's that time the Python scripts will run.
Since I've set up the pi as a access point, I'm not sure how to reverse it and allow it to connect to wifi again without ruining the access point and current set up, so I'm not sure if there's an easy way to download any packages or modules I may need.
Anyway, any help anyone could give me would be incredibly useful - many thanks!!
Unless you are planning for a production web server, for simple application like display sensor status or control sensor via web page, there is a simpler solution for beginners and for python programmers. Since you are using python, so you don't have to use PHP, and you probably don't need to have Nginx at this stage. There are actually two ways in my experiences to do it.
1) Using http.server
The 'simple way' to serve web page using python based on python standard library http.server, utilising python build-in socket based http server. But it is less intuitive to set it up for GET/POST requests/responses. It is too long to describe it here, but I have a blog post on how to do it here.
2) Using Flask web development micro framework
Flask allows you to setup html template, handling route and run a web server easily within python environment. You need to install Flask package for python web development. The simplest flask python code that addressed your question of serving the data to a web page would be:
from flask import Flask, render_template_string
app = Flask(__name__)
data = 200 #assuming this is the data you want to show in your web page
#app.route('/')
def index():
return render_template_string('''
<h1>My Sensor Web Page</h1>
<p>My sensor reading is {}".format(data))</p>
'''
if __name__ == '__main__':
app.run(debug=True)
Launch your browser and point it to http://localhost:8000, you should see the data to be rendered as webpage per our simple example code.
What you will need is to either import your code into this flask example, or integrate it into the example, and pass the data you want to display to render_template_string function.
I would suggest using a web framework to host the "website". Flask is one that I have used for similar applications. Since this method allows you to directly call python functions in response to http requests it should be fairly easy to implement what you are trying to do.
As a bonus, you can use flask with nginx but I really don't think you need it for this specific application.

invoke python script and populate results on webpage

I created a simple python script which takes a URL as input and once passed will do curl using multiple proxies and show the response code, now I want to create a webpage where others can use(my colleagues) as it will help them too, I want to create simple webpage which let them select set of proxy addresses, and input URL and upon submission, it will run the script on a machine(webserver) and populate the result to webpage using dynatable or datatable frameworks, but am not sure how or if it is possible as I didn't worked much in webserver thing, I want to know what tools I will need and how do I design it.
If python script can be called in terminal(as it needs to run curl) and show result on webpage based on output from script(which I will export to csv file), how can I do that? what to use xampp, wamp, lamp etc ?
You need a framework for this, something that will listen to your request coming from the front-end (webpage), there are tons out there as python framework, you can check bottle framework as a starting point.
So the flow would be something below.
1. from webpage, a request is sent to the backend
2. backend receive the request and run your logic (connecting to some server, computing logic, etc)
3. once backend process is done, backend then send the response to webpage
you can either use a REST approach or use templating functionality of the framework
You will need a request. You can do this in JavaScript with an ajax request there are frameworks to hook it up straight to your python which allow you not to code the JavaScript https://www.w3schools.com/xml/ajax_intro.asp there are many JavaScript frameworks that will make this easier/ shorter to code.

Streaming values in a python script to a wep app

I have a python script that runs continuously as a WebJob (using Microsoft Azure), it generates some values (heart beat rate) continuously, and I want to display those values in my Web App.
I don't know how to proceed to link the WebJob to the web app.
Any ideas ?
You have two main options:
You can have the WebJobs write the values to a database or to Azure Storage (e.g. a queue), and have the Web App read them from there.
Or if the WebJob and App are in the same Web App, you can use the file system. e.g. have the WebJob write things into %home%\data\SomeFolderYouChoose, and have the Web App read from the same place.
You would need to provide some more information about what kind of interface your web app exposes. Does it only handle normal HTTP1 requests or does it have a web socket or HTTP2 type interface? If it has only HTTP1 requests that it can handle then you just need to make multiple requests or try and do long polling. Otherwise you need to connect with a web socket and stream the data over a normal socket connection.

Communicating between appengine and an application

I'm working on a web interface which currently runs using PHP and communicates locally to a python script.
I'm moving the web side to appengine, which so far is going well when being used locally, I'm currently communicating from the appengine app to the python app via get requests that are handled by the python script.
The problem is, that obviously the machine running the python script will be behind a firewall, I've never needed to do this before and am not sure on how to implement this best.
The only idea I have so far is for the python script to send post requests to the appengine with some data and then as a response, send back some other data. The only problem with this is that the web interface should update the client quite fast.
Any ideas?
Take a look at ProtoRPC Python API: https://developers.google.com/appengine/docs/python/tools/protorpc/overview
Though it is still marked as experimental, it seems to be a decent framework for what you are trying to do - send messages back and forth between the apps.
Since you said your local app runs behind a firewall, I'm assuming you cannot open up an endpoint and protect it with some form of authentication.
Once you have messages flowing, you can either use Channel API to keep the front-end updated: https://developers.google.com/appengine/docs/python/channel/overview
Or if you want to go more basic, just implement long/short polling through AJAX.
Sorry with the limited amount of info you have provided, that's all I can think of right now. Please feel free to post more details and I'll try to help further.

Categories