Python doesn't run in CPanel / throws error - python

I am a PHP developer and was tasked with writing a Python script. I am lost.
Short summary:
My client wants to log which directory and file, on which URL was visited by which IP
My code:
import sys
import os
import io
import os.path
import socket
import requests
directory = os.getcwd()
ip = os.environ["REMOTE_ADDR"]
hostname = socket.gethostname()
i = socket.gethostbyname(hostname)
If I may ask two questions:
How do I run Python on Cpanel? I have uploaded the file example.py and navigated to the file, but the page only displays the code.
On the above code (in online code testers), I get an error: KeyError: 'REMOTE_ADDR'. What am I doing wrong

Related

Pyinstaller app not getting microphone permission works fine with terminal

Our script which uses the following libraries
import os
import sys
from random import sample as rand_sample
import PySimpleGUI as sg
from gtts import gTTS
from Levenshtein import ratio
from cryptography.fernet import Fernet
import platform
import webbrowser
from hashlib import sha1
from getpass import getuser
from socket import gethostname
import clipboard
import wavio
import sounddevice as sd
import soundfile as sf
import speech_recognition as sr
import wave
from playsound import playsound
from difflib import get_close_matches
import json
import urllib.request
from ssl import _create_unverified_context
class audio():
def __init__(mic,path):
mic.fs=44100
mic.path=path
def recordAudio(mic,time):
recording=sd.rec(int(time*mic.fs),samplerate=mic.fs,channels=1)
sd.wait()
return recording
def save(mic,filename,recording):
wavio.write(mic.path+filename,recording, mic.fs, sampwidth=2)
def play(mic,filename):
data,fs = sf.read(mic.path+filename,dtype='float32')
sd.play(data,fs)
status=sd.wait()
def getText(mic,filename): #use google speech recongition to convert
# print (mic,filename)
r = sr.Recognizer()
hellow=sr.AudioFile(mic.path+filename)
with hellow as source:
r.adjust_for_ambient_noise(source, duration = 1)
audio = r.record(source)
try:
s = r.recognize_google(audio)
return(s)
except:
return ('error reading speech...')
return ""
Can access microphone when run the app bundle via terminal (Permission is granted for it screenshot attached)
however when we package it using pyinstaller using this command
pyinstaller --noupx --onedir --onefile --windowed interview.spec interview.py
It is successfully converted into an app and we can launch it with double click. But it cannot access micriphone, nor does it asks for permission. Do we have to add some specific python snippet to ask for permission? Looking forward to your suggestions and help as i am clueless in this.
System:
macOS Big Sur
Version 11.5.2
In the end just ended up writing a script and putting an icon on it, so user can double click on it and start using the app. Though in Big Sur you have to do chmod 755 to it in almost every new pc it is copied to.
#!/bin/bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd "${DIR}"
./interview.app/Contents/MacOS/interview

How to open a remote file with GDAL in Python through a Flask application

So, I'm developing a Flask application which uses the GDAL library, where I want to stream a .tif file through an url.
Right now I have method that reads a .tif file using gdal.Open(filepath). When run outside of the Flask environment (like in a Python console), it works fine by both specifying the filepath to a local file and a url.
from gdalconst import GA_ReadOnly
import gdal
filename = 'http://xxxxxxx.blob.core.windows.net/dsm/DSM_1km_6349_614.tif'
dataset = gdal.Open(filename, GA_ReadOnly )
if dataset is not None:
print 'Driver: ', dataset.GetDriver().ShortName,'/', \
dataset.GetDriver().LongName
However, when the following code is executed inside the Flask environement, I get the following message:
ERROR 4: `http://xxxxxxx.blob.core.windows.net/dsm/DSM_1km_6349_614.tif' does
not exist in the file system,
and is not recognised as a supported dataset name.
If I instead download the file to the local filesystem of the Flask app, and insert the path to the file, like this:
block_blob_service = get_blobservice() #Initialize block service
block_blob_service.get_blob_to_path('dsm', blobname, filename) # Get blob to local filesystem, path to file saved in filename
dataset = gdal.Open(filename, GA_ReadOnly)
That works just fine...
The thing is, since I'm requesting some big files (200 mb), I want to stream the files using the url instead of the local file reference.
Does anyone have an idea of what could be causing this? I also tried putting "/vsicurl_streaming/" in front of the url as suggested elsewhere.
I'm using Python 2.7, 32-bit with GDAL 2.0.2
Please try the follow code snippet:
from gzip import GzipFile
from io import BytesIO
import urllib2
from uuid import uuid4
from gdalconst import GA_ReadOnly
import gdal
def open_http_query(url):
try:
request = urllib2.Request(url,
headers={"Accept-Encoding": "gzip"})
response = urllib2.urlopen(request, timeout=30)
if response.info().get('Content-Encoding') == 'gzip':
return GzipFile(fileobj=BytesIO(response.read()))
else:
return response
except urllib2.URLError:
return None
url = 'http://xxx.blob.core.windows.net/container/example.tif'
image_data = open_http_query(url)
mmap_name = "/vsimem/"+uuid4().get_hex()
gdal.FileFromMemBuffer(mmap_name, image_data.read())
dataset = gdal.Open(mmap_name)
if dataset is not None:
print 'Driver: ', dataset.GetDriver().ShortName,'/', \
dataset.GetDriver().LongName
Which use a GDAL memory-mapped file to open an image retrieved via HTTP directly as a NumPy array without saving to a temporary file.
Refer to https://gist.github.com/jleinonen/5781308 for more info.

Using python to run Latex compiler, why does it hang if there are errors in the latex?

I have a python script that takes the (latex source) content of a google doc and creates a pdf.
This is the function I use for the pdf:
# -*- coding: utf-8 -*-
#!/usr/bin/python
"""One of the main activiating files of IMPS - this downloads all the files in a directory, creates the input.tex file and archives them a tar file
TODO
Before we send to stackoverflow we should make sure that everthing is in a function and that the If __main__ trigger is working
I'd also like to have doc strings for all of the methods
"""
import os
import glob
import tarfile
import time
import datetime
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import urlparse
import argparse
import re
def generate_pdf(filename,destination=""):
"""
Genertates the pdf from string
from http://stackoverflow.com/q/19683123/170243
"""
import subprocess
import shutil
current = os.getcwd()
this_dir=os.path.dirname(os.path.realpath(__file__))
os.chdir(this_dir+"/latex")
proc=subprocess.Popen(['pdflatex',filename+'.tex'],stdout=subprocess.PIPE)
# subprocess.Popen(['pdflatex',tex])
temp=proc.communicate()
#Have to do it twice so that the contents pages are right
proc=subprocess.Popen(['pdflatex',filename+'.tex'],stdout=subprocess.PIPE)
temp=proc.communicate()
shutil.copy(filename+'.pdf',"../../live/"+destination+filename+ str(datetime.datetime.now()).replace(".", "-").replace(":", "_") + ".pdf")
trace_file = open("../../live/"+destination+"trace.txt", "w")
print >>trace_file, temp[0]
print >>trace_file, temp[1]
trace_file.close()
os.chdir(current)
Everything runs fine if the latex has NO errors, but if there is a problem, the function hands and nothing gets done. What I want is that problems are noted and exported into the trace. Any ideas what's going wrong?
When it encounters errors, pdflatex asks the user about how to proceed, so your script "hangs" because it is expecting input. Use pdflatex -interaction=nonstopmode -halt-on-error. See this TeX StackExchange question.
I think what you are missing is that you need to also need to setup a pipe for STDERR. This will let you see the error messages from pdflatex. You could also try explicitly setting the buffer size to zero when calling Popen.
self.child = subprocess.Popen(command
,bufsize=0
,stdout=subprocess.PIPE
,stderr=subprocess.PIPE)

Executing Python to write .csv file to remote directory

I have a script that processes downloaded log files into csv's per some parsing logic.
I want to write those csv's to a remote directory on a different server. This is due to space constraints on the server where I execute the script.
I have tried a few variations of the below script but I just cant seem to figure this out. I understand "SFTP" and "SSH" commands but I am not sure if that is the right approach in this use case.(I have all the keys and stuff setup to allow for remote connections between the servers)
import os
import re
import string
import csv
import gzip
import extract5
import pickle
import shutil
import pygeoip
def conn():
ssh = paramiko.SSHClient()
ssh.connect(XXXXXXXXXX.XXXX.XXXXXXXX.COM, username=XXXXXXX)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)
return conn(self:%s % (['cd /fs/fs01/crmdata/SYWR/AAM/AAM_Master/'])
downloadroot = '/opt/admin/AAM_Scripts/'
outgoing = conn
for sfile in os.listdir(downloadroot):
if sfile.endswith('gz'):
oname = sfile[sfile.find('AAM'):]
extract5.process(downloadroot,sfile,oname,outgoing)
#delete download dictionary and pickle
for date in os.listdir(downloadroot):
#match = re.match('(\d{4})[/.-](\d{2})[/,-](\d{2})$', date)
if date.find('AAM_CDF_1181_') > 0:
os.remove('%s%s' % (downloadroot, date))
os.remove('%sdownload.pkl' % (downloadroot))
Is what I am trying to do possible? I am on the right path or is my approach completely off. I would love some thoughts behind how or if I can accomplish this.

webbrowser.open() in python

I have a python file html_gen.py which write a new html file index.html in the same directory, and would like to open up the index.html when the writing is finished.
So I wrote
import webbrowser
webbrowser.open("index.html");
But nothing happen after executing the .py file. If I instead put a code
webbrowser.open("http://www.google.com")
Safari will open google frontpage when executing the code.
I wonder how to open the local index.html file?
Try specifying the "file://" at the start of the URL. Also, use the absolute path of the file:
import webbrowser, os
webbrowser.open('file://' + os.path.realpath(filename))
Convert the filename to url using urllib.pathname2url:
import os
try:
from urllib import pathname2url # Python 2.x
except:
from urllib.request import pathname2url # Python 3.x
url = 'file:{}'.format(pathname2url(os.path.abspath('1.html')))
webbrowser.open(url)

Categories

Resources