Execute python-script via Url - python

Is it possible to execute a python-script on a server without using something like django?
I mean I put script.py on host.com and want to call it like this:
http://www.host.com/script.py
The script then does something like calculating some variables and saving them on a mysql database.
edit: I assume I have to use something like cgi :-\

In short, yes. http://wiki.python.org/moin/CgiScripts. You'll have to either put your scripts in a cgi-bin folder or adjust the configuration for your web server.

Related

Is there a way to use a php server as a database?

I am attempting to make a small mafia style game, and I am using replit. Would there be a way to use a php server (or an html server) as a database that I can connect to from a python project?
HTML nor PHP are databases. The "LAMP" stack uses PHP with MySQL / MariaDB for its database, which might be what you're referring to... However, the "P" there could also be Python ¯\_(ツ)_/¯
What you need in Python is a Data Persistence layer, which could just be a simple CSV / JSON file; however pickle module is easier to work with native Python types.
sqlite module can be used if you want the data to be more portable to other frameworks/languages.
And the final option is to actually run your own database server externally and expose it over a remote TCP / HTTP API connection (I don't think Repl.it supports running Docker containers).
If you have access to the actual machine, you can run something like SQLite on the machine. You are running dangerously close to needing more security and what not though.
Security is important, but if you just want to "play around" running something like SQLite should answer your initial pass.

Is it possible to run a script using CRON that requires a redirect URI?

I created a Spotipy script that pulls the last 50 songs I listened to and adds them, and their audio features, to a Google Sheets. However, I'd love to be able to run this script daily without me manually having to run it, but I have very little experience with CRON scheduling. I'm having trouble wrapping my head around how it can be run given all of the command line arguments I need to enter.
The code requires multiple command line arguments passed first, such as
export SPOTIPY_REDIRECT_URI="http://google.com"
export SPOTIPY_CLIENT_SECRET='secret'
and similar for the client ID.
Additionally, the first argument after the script call is the username username = sys.argv[1].
Most importantly, it prompts me to copy and paste a redirect URL into the command line, which is unique each run.
Is it at all possible to pass the redirect URL to the command line each time the script is run using CRON?
I think what you're looking to accomplish can be achieved in one of two ways.
First, you could write a shell script to handle the export commands and passing the redirect URI to your script. Second, with a few tweaks to your script, you should be able to run it headlessly, without the intermediary copy/paste step.
I'm going to explore the second route in my answer.
Regarding the environment variables, you have a couple options. Depending on your distribution, you may be able to set these variables in the crontab before running the job. Alternatively, you can set the exports in the same job you use to run your script, separating the commands by semicolon. See this answer for a detailed explanation.
Then, regarding the username argument: fortunately, script arguments can also be passed via cron. You just list the username after the script's name in the job; cron will pass it as an argument to your script.
Finally, regarding the redirect URI: if you change your redirect URI to something like localhost with a port number, the script will automatically redirect for you. This wasn't actually made clear to me in the spotipy documentation, but rather from the command line when authenticating with localhost: "Using localhost as redirect URI without a port. Specify a port (e.g. localhost:8080) to allow automatic retrieval of authentication code instead of having to copy and paste the URL your browser is redirected to." Following this advice, I was able to run my script without having to copy/paste the redirect URL.
Add something like "http://localhost:8080" to your app's Redirect URI's in the Spotify Dashboard and then export it in your environment and run your script--it should run without your input!
Assuming that works, you can put everything together in a job like this to execute your script daily at 17:30:
30 17 * * * export SPOTIPY_REDIRECT_URI="http://localhost:8080"; export SPOTIPY_CLIENT_SECRET='secret'; python your_script.py "your_username"

Can I use Electron without node.js

Im new to Javascript so I would like to keep it at the bare minimum. Is there a way that I can use the Electron to communicate with python script without having node.js? My app is just a basic app that takes some input from users from a html page and I need this text input to be processed in python and write an excel file. So there is not much happening in html so is there a simple way to transfer the input to python file? I want to use Electron because I need this html to be my UI and also I need to distribute this app.
I guess the answer is "no": the main process running node will always be there.
An Electron app consists of a JavaScript main process, and one or more JavaScript renderer processes. There is no built-in Python support. And the user will need Python already installed. So, it sounds like a poor fit for what you need.
The answers here may be useful, and will show how to call the python script. I took a quick look at the flexx toolkit mentioned there. It seems to work with the user's browser, rather than producing a single executable.
Recently i have done it with some sort of trick hope it will help you and there are the following step which i followed-
Created a stand alone python exe using pyinstaller and the exe has flask server internally then i put the flask server inside my node application.
Now we have to initiate our flask server and send a request to it for processing, i have done this with the help of "execFile" function as a child process, for which i have created a function and the code was something like that-
async function callFlask(){
var child = require('child_process').execFile;
child('path_to_python_exe ', function(err, data) {
if(err){
console.error(err);
return;
}
});
}
Now we have initiated our flask server then will send the request with the help of fetch request like
await callFlask().then(
await fetch('host_ip_defined_in_flask'+encodeURIComponent('data'))
Now further we can extend our then chain to get response from python if any and proceed further forexample -
await callFlask().then(
await fetch('host_ip_defined_in_flask'+encodeURIComponent('data'))
.then(res => res.text())
.then(body => console.log(body)))
Here, your output data which python return will be printed in console then you can make your node application behave differently depending on output returned by it.
Also you can package your app with available packagers for electron like electron-packager it will work like a charm.
Also there is are some disadvantage for using python as like it will increase your package size and the process will be difficult to kill from electron after processing so it will increase burden on host machine.
I am assuming that Explaining to create a flask server is not the scope of this question instead if you face any issues let me know, i hope it will help...

Dynamically displaying a Python variable in a Flask application

I am working on a little personal project with the aim of using Python to obtain and process data from a running game and use HTML/CSS to output this data using Flask.
I am very new to Python, but have some basic exposure to HTML/CSS and JavaScript.
Using example code from the web and various tutorials I have been able to get two distinct sections of this working to a proof of concept stage:
My python code runs in an interuptible loop and will output a variable every X seconds to the console.
I have setup a Flask application that I can pass a variable to in a Python script and display in a browser.
My problem is I am stumped with how I go about joining these two together. In the Python script that runs the Flask app nothing else happens while the Flask app is running, I assume because it is executing synchronously. This means that I can't start the Flask app and then have my loop run after that, how can I get both to work simultaneously?
EDIT:
After more thinking about this and some further research I think I have framed the problem incorrectly due to the way I have setup my local environment.
My original thinking was that the Python script which runs the Flask server could also be used to recieve/process and output my game logic*. I now believe this to be incorrect because in typical usage Flask would be running on a web server somewhere serving the website while the data from the game would have to be collected and processed by an application local to the running of the game.
So, given the above the question could be better posed as:
How to dynamically display (say with a maximum refresh interval of 1s) a Python variable that is constantly changing using a Flask server? How do I get these two separate parts of my project to communicate with one-another?
*If this were possible (and I don't know its not) it would actually accomplish what I want from this project but is not likely to be a particularly useful skill so I would like to figure out how to do this the correct way in case this becomes something I would like to make available to others.

Generating and serving files created by a Python script on Heroku (Node/Express server)

I'm working on a site that collects textual input from a number of users and then gives all that input to a user with another role after a set amount of time. The user who is given access to all the input needs to be able to export all the text as a word document.
Right now, on my local machine, a button on the page makes a db call for all the text, and uses the fs npm module to write the correct set of input to a raw text document in a format the pyton script can understand. I then use the docx module in python to read the text and write the formatted input into the word document, saving it into the public directory in my server. I can navigate to it manually after that.
I can automate it locally by writing a simple cron job that waits for the contents of the raw text file to change, firing the python program when that happens and having the link to the word doc appear after some timeout.
My question is how would I get this to work on my heroku site? Simply having python isn't enough, because I need to install the docx module with pip. Beyond that, I still need to have a scheduled check for the raw text file to change to fire the python script. Can this be accomplished through the Procfile or some heroku addons? Is there a better way to accomplish the desired behavior of button click->Document creation->serve the file? Love to know your thoughts.
You have a few different issues to look at: 1) enabling both Python and Node and then 2) correct use of filesystem on Heroku and 3) ways to schedule the work.
For #1, you need to enable multiple build packs to get both Node.js and Python stacks in place. See https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app.
For #2, you need to send the files to storage service of some kind (e.g., Amazon S3) - the filesystem for your dyno is ephemeral, and anything written there will disappear after a dyno restart (which happens every 24 hours no matter what)
For #3, the simplest solution is probably the Heroku Scheduler add-on, which acts like a rudimentary cron. Remember, you don't have low-level OS access, so you need to use the Heroku-provided equivalent.

Categories