Execute Python commands on a server via a GET request - python

On My server, i have installed spleeter which is a python app. I am able to successfully call the spleeter commands in my terminal to execute my required commands.
I want to be able to execute those terminal commands with an http get request.
Basically, i have an android app thats supposed to make a Get request to my vps server which executes the spleeter commands and returns the response.
Please how can i create the Get endpoint. I need pointer please.

So, in the most simplistic sense, you can create a python web app (using Django, Flask or raw Python, etc.) that serves an HTTP endpoint. In that web app, you can do something like this that will run commands inside a python function, depending on the contents of the GET request:
import os
def run_command(command):
if command = "ls":
os.system('ls -l')
...
The only problem here is that, without authentication, this is horribly insecure. Anyone with access to the internet could call your endpoint and run those commands, unless you add some sort of authentication into your Android and/or Python app to validate requests.
Even passing a simple "token" in a custom HTTP header from the app to the web server might be better than nothing to verify it is a legit user sending the command.
The other problem here is that the Python (or web server) user has to have permissions to run the command, that is another security issue altogether.
Alternatively, you could have the endpoint store the commands it received via GET requests into a Redis queue or something, and then have a separate script running locally validate the commands and run them on the system so at least you separate the web portion from the server user portion.
Without more details, it is hard to know exactly what you want to accomplish.
Reference: https://janakiev.com/blog/python-shell-commands/

Related

How do i create a Python HTTP server to host a file inside it?

I'm trying to make a program. This program will have the functionality to host a file in an HTTP server but I couldn't find a way to make it.
This HTTP server will host a JSON file in it.
And I wanted to make a program that sends a request to that server and then reads everything inside that server directory.
I tried many attempts and messed around with Python but I can't find the correct way to make it
This is already included in the standard library. Just run this in your terminal from the directory you want to serve:
python -m http.server
This is not recommended for product use, however, if you need to run this in production it is better to use a real HTTP server like Apache or NGINX anway.

How can I restart my server using a request api

so I have a server and did an api for it so I can update patch files to my server, however now when I update the some batch files in the server, I always have to stop running the server and than run it again to see the changes, I was wondering what can I do so that my server restart it's self
Yes you can,
Make requests api send a json like {'do': 'refresh_server'}-
then just type exit(), then run the file again using the os module.
Edit: This is solution for windows

Remote API call working local but not working in production

I am creating my first web app and hosting it on Heroku's free version. It is an NBA statistics site and so as part of my application I make a call to a remote API to retrieve the stats for a certain player. When I run this on the local server via the 'heroku local' command my app works as intended with the call being able to be done in just a second or two. When I launch the app on the production server using the 'git push' command everything on my site works normal except when I attempt to make this call, it never finishes and I receive a timeout error. Does anybody know the cause of this issue and how I can fix it? Thanks

How to make two different python scripts to run on the same port?

Currently I'm developing web services in python using web.py, that serves different functions. Something like
BigQueryService.py
LogicImplementer.py
PostgreService.py
Each service works perfectly when running on the local machine. After deploying on the server, due to I'm refering an other python script, it returns a module error.
Since we have to run all the services on the same port, I pasted all the scripts into a single file named Engine and made it to work using the command
$ nohup python Engine.py 8080 &
Is there any better way to structure the service in web.py? Or is there a way to run all the individual scripts on the same port?
If each service creates its own listener/server socket on the port, then the answer is no. You will need to use the equivalent of an app server that has a single server port and distributes the incoming request to the relevant app (running on the server) based typically on the relative path - so e.g. http://myserver.net:8080/bqs paths get passed to your BiqQueryService, /li to LinkImplementor, /pgs to PostgreService. Flask will do something like this, I'm sure other web service frameworks will too. The server will handle all the communication stuff, pass requests to the app (e.g. bqs) and handle sending response to the client.

Google Analytics API access without local browser in python

I want to query the Google Analytics API using Python to periodically download data from my Analytics account and store data in a local database. I am basically following the steps as given in the basic tutorial. I am using the Google client API library for Python in this process.
My script is working fine so far when I am running it on my local dev machine (Mac). When I start the script, my browser opens and I am prompted to grant access to my Analytics data from the app. Afterwards I can run my script as often as I want and get access to my data.
On my server (Ubuntu, only terminal available), the w3m browser opens, but I cannot access my Google account from there. I can only quit w3m and kill the program with Ctrl-C. There is an error message like:
Your browser has been opened to visit:
https://accounts.google.com/o/oauth2/auth?scope=some_long_url&access_type=offline
If your browser is on a different machine then exit and re-run this
application with the command-line parameter
--noauth_local_webserver
However when I run my script with the parameter --noauth_local_webserver, I get the same results - w3m opens and I cannot authenticate.
How can I get the --noauth_local_webserver to work? I there another way to authenticate without a local browser on the same machine?
When you use FLAGS = gflags.FLAGS, you actually need to pass the command-line arguments to FLAGS (this may or may not have tripped me up as well :) ). See here for an Analytics-centric example of how to do it (code below as links tend to go away after a while). General idea is that argv arguments are passed into the FLAGS variable, which then become available to other modules.
# From samples/analytics/sample_utils.py in the google-api-python-client source
def process_flags(argv):
"""Uses the command-line flags to set the logging level.
Args:
argv: List of command line arguments passed to the python script.
"""
# Let the gflags module process the command-line arguments.
try:
argv = FLAGS(argv)
except gflags.FlagsError, e:
print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS)
sys.exit(1)
# Set the logging according to the command-line flag.
logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
Also, turns out that we aren't alone! You can track this bug to see when this will get added the documentation.
you can also use GA as a service API:https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-py
this works perfectly fine. Just remmeber to convert the p12 to an unencryptet PEM file using openssl
$openssl pkcs12 -in client_secrets.p12 -nodes -nocerts > client_secrets.pem
the import password is printed out when you download the P12 from google developer's console
I ran into the same issue and managed to solve it by SSHing into my server. Example:
ssh -L 8080:127.0.0.1:8080 <server-name>
I then ran my script through SSH. When I was presented with the URL (https://accounts.google.com/o/oauth2/auth?scope=some_long_url&access_type=offline), I copied and pasted into the browser on my machine to complete the authentication flow.
I ran it on my PC, got a token.json, and just copied the token on the server in the home folder (think working directory of the script), it solved it.
No authentication needed if you use same token

Categories

Resources