How to run a terminal command using python script, which is held through wsgi process? - apache

I have a Centos 7 server with cPanel and I'm working on a Telegram bot for my business needs. The bot should be able to run a terminal command with os.system or subprocess.Popen, however both options do not work when configured through a webhook + wsgi process.
I tested both with bot.polling method and they worked as a charm, however after I switched to webhook method served by flask and wsgi, both stopped working for me. I have tried the following:
mycommand = "python3.6 GoReport.py --id 31-33 --format word"
os.chdir('dir_to_run_command_from')
os.system(mycommand)
and the following one:
mycommand = "python3.6 GoReport.py --id 31-33 --format word"
subprocess.Popen(mycommand, cwd="dir_to_run_command_from", shell=True)
Both options simply do nothing right now. I tried to print them both and received 0 as a response. I wonder if the issue is caused by permissions or something.
I expect both options to work through webhook + wsgi as good as they work through bot.polling method.

I think I got it wrong. Your script writes a report to a specific directory. You do not need a result in your application route.
I wrote a small test application called tryout. It runs in a virtual environment.
$ mkdir tryout
$ cd tryout
$ python3 -m venv tryout
$ source tryout/bin/activate
$ export FLASK_APP=tryout/app
$ export FLASK_ENV=development
$ flask run
Directory structure:
/tryout
/app/*
/bin/*
/include/*
/lib/*
/subdir/*
Application:
# /tryout/app/__init__.py
import sys, os
from flask import Flask
def create_app(env=os.getenv('FLASK_ENV', 'development')):
app = Flask(__name__)
#app.route('/run-script')
def run_script():
import subprocess
cmd = 'python script.py'
cwd = 'subdir'
ret = subprocess.check_output(cmd, cwd=cwd, shell=True)
print(ret)
return ret, 200
return app
app = create_app()
Script:
# /subdir/script.py
import os, sys
def main():
with open('report.txt', 'w+') as fp:
fp.write('Info\n')
sys.stdout.write('It works!')
if __name__ == '__main__':
main()
It works!
A new file named "report.log" is written into the "subdir"-directory.
In Browser appears "It works!".
Hope I could help you or I have no real idea of what you want to do.

If you want to run an external script from inside flask, you could use subprocess to run the script from the command line. This is the right solution.
#app.route('/run-script')
def run_script():
cmd = '<your command here!>'
result = subprocess.check_output(cmd, cwd='<your wordir>', shell=True)
return render_template('results.html', **locals())
Have fun!

#Bogdan Kozlowskyi
Is it possible to pipe on the command line? Do you need to return a result to the user?
cmd = 'first_cmd | tee report.log'
result = subprocess.check_output(cmd, cwd='<your wordir>', shell=True)
Perhaps you should look for shell commands like '>>', '>' and 'tee'.

Seems to be a user-groups permission problem (execute and write).

Related

Permission denied in colab

I want to use stockfish in my colab notebook.
I first tried with the chess.engine.SimpleEngine.popen_uci() command, which seems to be outdated, since module 'chess.engine' has no attribute 'SimpleEngine'
So I tried with stockfish itself:
from stockfish import Stockfish
stockfish = Stockfish('/usr/local/lib/python3.7/dist-packages/stockfish')
Here I always get the error:
Permission denied: '/usr/local/lib/python3.7/dist-packages/stockfish'
I googled for it and came up with some good tries:
I tried with !chmod +x '/usr/local/lib/python3.7/dist-packages/stockfish' which compiled, but didn't resolved the problem, and !chmod +x 'stockfish', which didn't find the folder.
So how exactly do I give permission to use the stockfish folder?
You need to set the os.setuid to 0 like this code below:
! wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-linux-x86_64.tar.gz -q
! tar -xzf elasticsearch-7.9.2-linux-x86_64.tar.gz
! chown -R daemon:daemon elasticsearch-7.9.2
import os
from subprocess import Popen, PIPE, STDOUT
es_server = Popen(['elasticsearch-7.9.2/bin/elasticsearch'],
stdout=PIPE, stderr=STDOUT,
preexec_fn=lambda: os.setuid(0) # as daemon
)
# wait until ES has started
! sleep 30

Flask API run on Docker but can't access

I'm trying to run my API with Docker. I've managed to build and run it but, when I test it in Postman, it doesn't work.
I have a folder that contains uhopper.py(which is the API), requirements.txt and Dockerfile.
Everything seems to work but when I make a GET request on "http://127.0.0.1:5000/profile/john" it doesn't give a response and an error occurs ("There was an error connecting to http://127.0.0.1:5000/profile/john.")
I hope someone can help me, thanks in advance!
uhopper.py
from flask import Flask, request
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
john = {
"http://en.wikipedia.org/wiki/Category:Writers_from_Belfast": 1,
"http://en.wikipedia.org/wiki/Category:People_educated_at_Newstead_Wood_School": 2,
}
mac = {
"http://en.wikipedia.org/wiki/Category:1998_establishments_in_New_York": 1,
"http://en.wikipedia.org/wiki/Category:Public_Interest_Research_Groups": 1,
}
class Profile(Resource):
def get(self, name):
if name == 'john':
return john, 200
elif name == 'mac':
return mac, 200
return "Not found", 400
api.add_resource(Profile, "/profile/<string:name>")
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
requirements.txt
Flask>=1.1.1
Flask-RESTful>=0.3.7
Dockerfile
FROM python:3.7
WORKDIR /uhopper
COPY . .
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["uhopper.py"]
This is my Git Bash:
You're missing the Flask import:
from flask import Flask
You can reorder the Dockerfile to save duplicating the path:
FROM python:3.7
WORKDIR /uhopper
COPY . .
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["uhopper.py"]
I think it's good practice to use the absolute path CMD ["/uhopper/uhopper.py"]
I encourage you to specify (at least minimum) versions of imports too:
Flask>=1.1.1
Flask-RESTful>=0.3.7

why isn't chatterbot working in this code?

I have a code that imports chatterbot.
I run
python -m spacy download en
python terminal1.py.
terminal1.py
''''
import spacy
from chatterbot import ChatBot
# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)
# Create a new instance of a ChatBot
bot = ChatBot(
'Terminal',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
'chatterbot.logic.MathematicalEvaluation',
'chatterbot.logic.TimeLogicAdapter',
'chatterbot.logic.BestMatch'
],
database_uri='sqlite:///database.db'
)
print('Type something to begin...')
# The following loop will execute each time the user enters input
while True:
try:
user_input = input()
bot_response = bot.get_response(user_input)
print(bot_response)
# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
break
''''
I get the error
OSErrror: [E050] Can't find model 'en'. It doesn't seem to be a shortcut link, a Python package or a valid path to a data directory.
Can some one help me fix this problem and in the process run the code. I will be much thankful.
try to run this command pip install spacy && python -m spacy download en this will link en model.

scrapy shell url return SyntaxError in iPython notebook

In windows power shell ,I can run scrapy shell 'http://www.hao123.com',
scrapy shell 'http://www.hao123.com
I can run ipython
I can run ipython but not scrapy shell 'http://www.hao123.com'
ipython then scrapy shell 'http://www.hao123.com
In ipython notebook,I can't run scrapy shell 'http://www.hao123.com'also
scrapy shell 'http://www.hao123.com'
File "<ipython-input-3-be4048c8f90b>", line 1
scrapy shell 'http://www.hao123.com'
^
SyntaxError: invalid syntax
Ipython is installed by anaconda,scrapy is installed by pip,anaconda and pip is in different file.
Please help me!
That's not a feature you can have in ipython. scrapy shell is a command, it's own application completely separate from ipython.
However, there are two things you can do:
If you have a Spider and Response objects from somewhere you can simply use scrapy.shell.inspect_repsonse
from scrapy.shell import inspect_response
# You need a scrapy spider object that has crawler instance attached to it
some_spider = Spider()
# you need response object
some_response = Response()
inspect_response(some_spider, some_response)
# new ipython shell will be embedded, like one from scrapy shell command
Otherwise you can spawn a subprocess:
import subprocess
subprocess.call('scrapy shell http://stackoverflow.com', shell=True)
# new ipython shell will be embedded, like one from scrapy shell command
if you are using Anaconda on windows 7 .
Follow: environment --> root --> Open terminal. Then you can write:
scrapy shell 'http://www.hao123.com'

Write echo output in file when shell execute from os.system

I am running a shell command through jython os.system command. My code is given below:
import os
import sys
vCmdLine = "sh C:/scripts/WAQ.sh PARAM_1 PARAM_2 >> C:/logs/WAQ.log 2>&1"
vCmdRC = os.system(vCmdLine)
My shell has several echo commands which I want to get written in C:/logs/WAQ.log file.
The code is working fine if I execute the same on unix OS. However when I run the same on windows, it does not write anything in C:/logs/WAQ.log file. I am using unix util lib on windows.
What is the issue?
I have changed a code and replace os.system with subprocess.call and the logging is now perfect. Below is my code
import subprocess
vLogFileName = 'C:/logs/WAQ.log'
vCmdLine = "sh C:/scripts/WAQ.sh PARAM_1 PARAM_2"
f = open(vLogFileName, "a")
subprocess.call(vCmdLine, stdout=f)
f.close()
Why the logging is not working with os.system on windows?