I'm running this python script wherein the output of the python script will be display in web browser so I'm using PHP for this. I'm using netmiko in python for upgrading the router. The main problem is when I run the python script in CMD it perfectly works but when I run it in Web browser using some html and php not all the script is not running. It gives me the below error:
scp.SCPException: scp: c7200-ipbasek9-mz.150-1.M10.bin: No such file or directory
This is the code in python with netmiko
from netmiko import ConnectHandler, cisco
from netmiko import FileTransfer
from netmiko import SCPConn
from datetime import datetime
import sys
ip_addr = Type[0]
device_type = Type[1]
password = Type[2]
source_file = Type[3]
print ("IP:" + ip_addr + '\n'
"Model:" + device_type +'\n'
"IOS:" + source_file + '\n')
start_time = datetime.now()
s_file = source_file
d_file = source_file
net_device = {
'device_type': 'cisco_ios',
'ip': ip_addr,
'username': 'admin',
'password': password,
'port': 22,
}
ssh_conn = ConnectHandler(**net_device)
print ("\n\n")
print ("dir")
output = ssh_conn.send_command("dir")
print (">> " + output + '\n')
scp_conn = SCPConn(ssh_conn)
print ("Copying files...\nPlease wait for a while... \n")
scp_conn.scp_transfer_file(s_file, d_file)
When I run this in web browser, it stops here in the copying files and it gives me this error:
Related
I have a python code on RaspberryPi 4. If I run It from terminal, It works correctly, but if I want to run It from php code, It not works. What is the problem with It? Why run it correctly in terminal, but not run from php? Thanks.
The python code part 1:
#!/usr/bin/python3
import os
import glob
import time
import board
import Adafruit_DHT
import urllib.request
from urllib.request import urlopen
from datetime import datetime
import socket
import re
import smtplib
from email.message import EmailMessage
#email
def email_alert(subject, body, to):
msg = EmailMessage()
msg.set_content(body)
msg['subject'] = subject
msg['to'] = to
user = "xyz#gmail.com"
msg['from'] = user
password = "abcdef"
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(user, password)
server.send_message(msg)
server.quit()
# DECLARATIONS
now = datetime.now() # Get time right now
timestamp = now.strftime("%Y-%m-%d-%H:%M:%S") # Format the timestamp
os.system('modprobe w1-gpio') # Adding 1-wire module to the kernel
os.system('modprobe w1-therm') # Adding 1-wire therm module
base_dir = '/sys/bus/w1/devices/' # Setting the base_dir
device_folder = glob.glob(base_dir + '28*')[0] # Check if a Dallas DS18B20 is connected
device_file = device_folder + '/w1_slave' # This file holds the temperature
#sensor_type = 11 #DHT sensor type 11/22
#sensor_pin = 7 #DHT sensor pin in GPIO
DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 22 # = GPIO22; az alap a GPIO4
# Initial the dht device, with data pin connected to:
#dhtDevice = adafruit_dht.DHT11(board.D23)
The python code part 2:
# Function to read raw temperature from the DS18b20
# It opens the device_file (/w1_slave), and reads the content, then close the file
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
# Function to read the temperature and format it to Celsius and Fahrenheit
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equal_pos = lines[1].find('t=')
if equal_pos != -1:
temp_string = lines[1][equal_pos+2:]
temp_c = float(temp_string) / 1000.0
global celsius
celsius = temp_c
temp_f = temp_c *9.0 / 5.0 + 32.0
return temp_c, temp_f
# Function that reads the humidity and temperature from the DHT11
# It only saves and use the humidity
#humidity = 75
def read_humidity():
global humidity
#global temperature
#humidity, temperature = Adafruit_DHT.read_retry(sensor_type, sensor_pin)
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
time.sleep(2)
while True:
if humidity is not None and temperature is not None:
#print ('Data from sensor is OK. Humidity= {0:0.1f} %'.format(humidity))
print("Temp={0:0.1f}*C Humidity={1:0.1f}%".format(temperature, humidity))
if humidity > 100:
read_humidity()
else:
break
else:
print ('Error getting data from DHT.')
# Function to get the local hostname
# It is used for identify the "senor" in the mySQL database
def get_host_name():
global local_hostname
local_hostname = socket.gethostname()
# Function that get the external IP-adress
def get_external_ip_address():
global external_ip
url = "http://checkip.dyndns.org" # This site return one line of text.
my_request = urlopen(url).read() # Read the URL
res = re.findall(b'\d{1,3}', my_request) # Search and findall integers in my_request
my_ip_list = list(map(int, res)) # Clean up the list
my_ip = str(my_ip_list)[1:-1] # Remove the square brackets
temp_ip = my_ip.replace(",", ".") # Replace comma with periods
external_ip = temp_ip.replace(" ", "") # Replace <space> with none-space
print ("External IP: " +external_ip) # Print the External IP address as xxx.xxx.xxx.xxx
# Function that actualle sends data and adds it to the database
def send_data():
print (timestamp) # For debug purpose
print (celsius) # For debug purpose
print (humidity) # For debug purpose
print (local_hostname) # For debug purpose
output = "http://example.com/rpi/add_temp.php?temp="+str(celsius) \
+"&humi="+str(humidity)+"&time="+str(timestamp)+"&sensor="+str(local_hostname)+"&ip=" \
+str(external_ip) # This is the string that is called by the urlopen
print (output) # For debug purpose
html = urlopen(output).read() # Actually performing the call
print (html) # For debug purpose
def main():
read_temp()
read_humidity()
get_host_name()
get_external_ip_address()
send_data()
#dhtDevice.exit()
#GPIO.cleanup()
email_alert("Hey", "Hello World", "xyz#gmail.com")
#email_alert("Hey", "Hello World", "1234567891#vtext.com")
if __name__ == "__main__":
main()
The php code:
<?php
// define the GPIO pins for the motor ouptput (Note: PiFace pins start at 200)
$output = shell_exec('/var/www/html/ds18b20_dht_mysql_v01.py 2>&1');
echo "<pre>$output</pre>";
?>
The error message what I get:
Traceback (most recent call last):
File "/var/www/html/ds18b20_dht_mysql_v01.py", line 138, in
main()
File "/var/www/html/ds18b20_dht_mysql_v01.py", line 128, in main
read_humidity()
File "/var/www/html/ds18b20_dht_mysql_v01.py", line 82, in read_humidity
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/common.py", line 94, in read_retry
humidity, temperature = read(sensor, pin, platform)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/common.py", line 81, in read
return platform.read(sensor, pin)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/Raspberry_Pi_2.py", line 34, in read
raise RuntimeError('Error accessing GPIO.')
RuntimeError: Error accessing GPIO.
I can see that the problem is the DHT11 temp sensor code. But I dont understand why work it in terminal and why not works it from php.
In your method read_humidity() is a call to itself. Is causes a circular reference...
I tried to run this .py file directly from Android. But it does not work.
.py file location is /var/www/html/ on apache server
String command = "python3 http://192.168.1.49:8081/ds18b20_dht.py";
try {
Process p = Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
I am working on this project that requires me to upload pictures on PHP, execute the picture on python, fetch the output from python and display it again on PHP.
PHP code:
<?php
$command = shell_exec("python C:/path/to/python/KNNColor.py");
$jadi = json_decode($command);
var_dump($jadi);
?>
Python code:
from PIL import Image
import os
import glob
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import io, color
from scipy.stats import skew
#data train untuk warna
Feat_Mom_M = np.load('FeatM_M.npy')
Feat_Mom_I = np.load('FeatM_I.npy')
Malay_Col_Train = Feat_Mom_M
Indo_Col_Train = Feat_Mom_I
#Data warna
All_Train_Col = np.concatenate((Malay_Col_Train, Indo_Col_Train))
Y_Indo_Col = [0] * len(Indo_Col_Train)
Y_Malay_Col = [1] * len(Malay_Col_Train)
Y_Col_Train = np.concatenate((Y_Malay_Col, Y_Indo_Col))
Train_Col = list(zip(All_Train_Col, Y_Col_Train))
from collections import Counter
from math import sqrt
import warnings
#Fungsi KNN
def k_nearest_neighbors(data, predict, k):
if len(data) >= k:
warnings.warn('K is set to a value less than total voting groups!')
distances = []
for group in data:
for features in data[group]:
euclidean_dist = np.sqrt(np.sum((np.array(features) - np.array(predict))**2 ))
distances.append([euclidean_dist, group])
votes = [i[1] for i in sorted(distances)[:k]]
vote_result = Counter(votes).most_common(1)[0][0]
return vote_result
image_list = []
image_list_pixel = []
image_list_lab = []
L = []
A = []
B = []
for filename in glob.glob('C:/path/to/pic/uploaded/batik.jpg'):
im=Image.open(filename)
image_list.append(im)
im_pix = np.array(im)
image_list_pixel.append(im_pix)
#ubah RGB ke LAB
im_lab = color.rgb2lab(im_pix)
#Pisah channel L,A,B
l_channel, a_channel, b_channel = cv2.split(im_lab)
L.append(l_channel)
A.append(a_channel)
B.append(b_channel)
image_list_lab.append(im_lab)
<The rest is processing these arrays into color moment vector, it's too long, so I'm skipping it to the ending>
Feat_Mom = np.array(Color_Moment)
Train_Set_Col = {0:[], 1:[]}
for i in Train_Col:
Train_Set_Col[i[-1]].append(i[:-1])
new_feat_col = Feat_Mom
hasilcol = k_nearest_neighbors(Train_Set_Col, new_feat_col, 9)
import json
if hasilcol == 0:
#print("Indonesia")
print (json.dumps('Indonesia'));
else:
#print("Malaysia")
print (json.dumps('Malaysia'));
So as you can see, There is only one print command. Shell_exec is supposed to return the string of the print command from python. But what I get on the "var_dump" is NULL, and if I echo $jadi, there's also nothing. Be it using print or the print(json) command
The fun thing is, when I try to display a string from this python file that only consists 1 line of code.
Python dummy file:
print("Hello")
The "Hello" string, shows up just fine on my PHP. So, is shell_exec unable to read many codes? or is there anything else that I'm doing wrong?
I finally found the reason behind this. In my python script there are these commands :
Feat_Mom_M = np.load('FeatM_M.npy')
Feat_Mom_I = np.load('FeatM_I.npy')
They load the numpy arrays that I have stored from the training process in KNN and I need to use them again as the references for my image classifying process in python. I separated them because I was afraid if my PHP page would take too long to load. It'd need to process all the training data, before finally classifying the uploaded image.
But then when I execute my python file from PHP, I guess it returns an error after parsing those 2 load commands. I experimented putting the print command below them, and it stopped showing on PHP. Since it's all like this now, there's no other way than taking the worst option, even if it'd cost me long loading time.
I tested this in the console:
php > var_dump(json_decode("Indonesia"))
php > ;
php shell code:1:
NULL
php > var_dump(json_decode('{"Indonesia"}'))
php > ;
php shell code:1:
NULL
php > var_dump(json_decode('{"Indonesia":1}'))
php > ;
php shell code:1:
class stdClass#1 (1) {
public $Indonesia =>
int(1)
}
php > var_dump(json_decode('["Indonesia"]'))
php shell code:1:
array(1) {
[0] =>
string(9) "Indonesia"
}
you have to have it wrapped in {} or [] and it will be read into an object or an array.
After an error you can run this json_last_error() http://php.net/manual/en/function.json-last-error.php and it will give you an error code the one your's returns should be JSON_ERROR_SYNTAX
Python
I have a python code
import paramiko
import time
import sys
import os
import pdb
ip = "1.1.1.1"
un = "root"
pw = "123"
key_filename='/path/to/id_rsa'
def ssh_con (ip, un, pw):
global ssh
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=un, password=pw, key_filename=key_filename)
def cmd_io (command):
global ssh_cmd
ssh_cmd.send("%s \n" %command)
time.sleep(1)
output = ssh_cmd.recv(10000).decode("utf-8")
print (output)
ssh_con(ip,un,pw)
ssh_cmd = ssh.invoke_shell()
cmd_io("clientsim cli")
time.sleep(2)
cmd_io("stop subscriber-group dth-sub stop-traffic udp")
time.sleep(1)
cmd_io("stop subscriber-group dth-sub")
time.sleep(1)
cmd_io("q")
cmd_io("exit")
PHP
I'm trying to run it via PHP
I've tried
$command = "python ".public_path().'/python/start_clientsim.py 2>&1';
$result = shell_exec($command);
I kept getting
Traceback (most recent call last):↵ File "/Applications/MAMP/htdocs/code/site/portal/public/python/stop_clientsim.py", line 32, in ↵ cmd_io("clientsim cli")↵ File "/Applications/MAMP/htdocs/code/site/portal/public/python/stop_clientsim.py", line 28, in cmd_io↵ print (output)↵UnicodeEncodeError: 'ascii' codec can't encode characters in position 360-362: ordinal not in range(128)↵
But if I run
python /Applications/MAMP/htdocs/code/site/portal/public/python/stop_clientsim.py 2>&1
on my Terminal directly, it works perfectly fine.
What did I do wrong here ?
How would one go about and debug this further ?
I am running a Python script from a PHP file. For a very a simple example, it works perfectly. Here is the Python script:
#!/usr/bin/env python
def test():
x = 2 + 1
print x + 4
return x
print test()
And PHP script:
<?php
$command = escapeshellcmd('/usr/bin/python2.7 /home/super/PycharmProjects/img_plus_text/helloworld.py');
$output = shell_exec($command);
echo $output;
echo "Finishing....!";
?>
Unfortunately, for more complicated script, It does not work. Here is the Python script
#!/usr/bin/env python
import numpy as np
import os
import random
import caffe
import cv2
from sklearn.externals import joblib
import create_dataset_final_mlp
import text_2_bow
def create_class_mapping():
with open("text_processing/encoded-classes.txt") as class_file:
lines = class_file.readlines()
dictionary = {}
for item in lines:
item = item.split(" ")
dictionary[item[0]] = item[1].replace("\n", "").replace("#", "")
return dictionary
def get_top5(probability_list):
result = []
probabilities = []
for i in range(0, 5):
top = probability_list.index(max(probability_list))
probabilities.insert(i, probability_list[top])
probability_list[top] = -1
result.insert(i, top)
return result, probabilities
def select_random_file(base_dir):
# Select random category
file_chosen = random.choice(os.listdir(base_dir))
# Select random file in that category
file_chosen = random.choice(os.listdir(base_dir + "/" + file_chosen))
return file_chosen
def get_top5_class_name(mapping, top5_list):
class_name_list = []
for i in range(0, 5):
index = top5_list[i]
class_name = mapping[str(index)]
class_name_list.insert(i, class_name)
return class_name_list
def main(image_to_classify):
dataset_path = "/images-test1"
image_file_chosen = create_dataset_final_mlp.find(image_to_classify, dataset_path)
if image_file_chosen is None:
image_file_chosen = select_random_file("%s" % dataset_path)
identifier = image_file_chosen.split(".")[0]
print "Identifier: " + identifier
dictionary = text_2_bow.create_dictionary("text_processing/dictionary-1000-words.csv")
# load CNN model from disk
classifier = create_dataset_final_mlp.load_model_from_disk("test/deploy.prototxt",
"test/snapshot_iter_1020.caffemodel", '256,256',
"test/converted_mean.npy")
# load the final MLP to classify patterns
print "loading model from disk..."
mlp = joblib.load("trained_model_9097_5000_3000_iter_100.model")
with open("export-ferramenta.csv") as export_file:
for line in export_file:
if identifier in line:
pattern = line
print "Pattern: " + pattern
break
# Show selected image
full_img_path = create_dataset_final_mlp.find(image_file_chosen, dataset_path)
# img = cv2.imread(full_img_path, cv2.IMREAD_COLOR)
# cv2.imshow("Image selected", img)
# cv2.waitKey(0)
# get the CNN features
inputs = [caffe.io.load_image(full_img_path)]
# the second parameter is used to switch for prediction from center crop alone instead of averaging predictions across crops (default).
classifier.predict(inputs, False)
features = classifier.blobs['fc6'].data[0]
extracted_cnn_features = create_dataset_final_mlp.get_cnn_features_as_vector(features)
extracted_cnn_features = extracted_cnn_features[:-1]
extracted_cnn_features = extracted_cnn_features.split(",")
# transform the text using text_2_bow functions
pattern = pattern.split(",")
text_pattern = pattern[1] + pattern[2]
matrix = text_2_bow.get_matrix(text_pattern, dictionary)
reshaped_matrix = np.reshape(matrix, 5000).tolist()
full_features_vector = extracted_cnn_features + reshaped_matrix
full_features_vector = np.asarray(full_features_vector, np.float64)
# use the MLP to classify
prediction = mlp.predict_proba([full_features_vector])
tolist = prediction[0].tolist()
mapping = create_class_mapping()
top5, probabilities = get_top5(tolist)
top_class_name = get_top5_class_name(mapping, top5)
print "Top 5 classes: ", top_class_name
print "Top 5 probs ", probabilities
return top_class_name, probabilities
if __name__ == "__main__":
main("")
PHP Script
<?php
echo exec('sudo /home/super/bin/caffe-nv/python /home/super/PycharmProjects/img_plus_text/demo_website.py 2>&1');
?>
ERROR
sh: 1: /home/super/bin/caffe-nv/python: Permission denied
I have read majority of the questions on the issue however I am stuck here. Please point out the mistake. Thank you.
The problem was with environment variables, I made a shell script as shown below
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/:$LD_LIBRARY_PATH
export PYTHONPATH=$PYTHONPATH:/home/super/bin/caffe-nv/python/
python /demo_website2.py $1
Then I run the shell script from PHP using exec
exec("/opt/lampp/htdocs/test/run.sh ". $param, $output);
I'm trying to get data from php script using a my python script:
#!/usr/bin/python
import urllib
import urllib2
url = 'https://example.com/example.php'
data = urllib.urlencode({'login' : 'mylogin', 'pwd' : 'mypass', 'data' : 'mydata'})
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
d = response.read()
print d
doesn't run with error:
ERROR=1704
php script accepts:
url: https://example.com/example.php?login=xxxxxxx&pwd=xxxxxxx&t=3
Isn't it because it is https, as described in the most voted response here: python ignore certicate validation urllib2