-2
我想連接遠程服務器使用python從同一個服務器從PHP獲取數據。從python的SSH連接和從PHP讀取文件?
我想連接遠程服務器使用python從同一個服務器從PHP獲取數據。從python的SSH連接和從PHP讀取文件?
import sys
import os
import paramiko
import time
import datetime
import json
#Define Server Detail for sftp
hostname = "hostname"
username = "username"
password = "password"
serverpath = "/var/www/projectfolder"
server, user, password, port = (hostname, username, password, 2020)
ssh = paramiko.SSHClient()
paramiko.util.log_to_file('paramiko.log')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(server, username=user, password=password, port=port)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls /tmp')
error = ssh_stderr.read()
# print "err", (error), len(error)
sftp = ssh.open_sftp()
# data = sftp.listdir()
data = sftp.listdir(serverpath)
dir_paths = []
# for i in data:
# dir_paths.append(i)
# print (i)
for line in data:
if line.endswith('.mp4'):
print(line)
#The local path where files to be downloded
#local_path = "/root/backup/"
#The Remote path of the Server
#sftp.chdir(remote_path)
#remote_path = '/root/backup/'
#print "changed"
# for filename in (sftp.listdir()):
#
# fullpath = os.path.join(remote_path, filename)
# # get timestamp of file in epoch secon
# timestamp = sftp.stat(fullpath).st_atime
# createtime = datetime.datetime.now()
# now = time.mktime(createtime.timetuple())
# datetime.timedelta = now - timestamp
#
# #Download the latest file
# if datetime.timedelta < 86400:
# print filename
#
# localpath = os.path.join(local_path, filename)
# print "downloading %s" % filename
# sftp.get(filename, localpath)
# print "downloading %s completed" % filename
sftp.close()
ssh.close()
So this is my code and i want to execute it from php script.
SO是不是一個地方保持個人筆記 – bansi
謝謝,它不是一個地方爭論,但解決問題的答案。 – user6795317
@ user6795317,SO不是代碼服務網站。請向我們展示您所做的事情,我們很樂意爲您提供幫助。 – Ronald