如何使用 Python 在 FTP 服务器中 运行 Python 脚本?
How to run Python script in FTP server using Python?
我想 运行 Python 脚本位于服务器上,使用另一个 Python 脚本和 FTP。
下面是我想要 运行.
的 (hello.py
) 文件位置
from ftplib import FTP
ftp = FTP('host')
ftp.login(user='user', passwd = 'password')
print("successfully connected")
print ("File List:")
files = ftp.dir()
print (files)
您可以使用 system() 调用:
system('python hello.py')
注意:使用 hello.py 脚本的完整路径。
另一种方法是使用 subprocess 模块。
最好使用runpy模块
import runpy
runpy.run_path("code.py")
有关详细信息,请参阅文档页面runpy module documentation
你不能 运行 FTP 服务器上的任何东西,无论如何。
看到一个类似的问题:Run a script via FTP connection from PowerShell
是关于PowerShell的,不是Python,不过没关系
您将需要另一个访问权限。类似于 SSH shell 访问。
如果您真的想从 FTP 服务器下载脚本并 运行 在本地机器上使用:
with open('hello.py', 'wb') as f
ftp.retrbinary('RETR hello.py', f.write)
system('python hello.py')
如果您不想将脚本存储到本地文件,这也可以:
r = StringIO()
ftp.retrbinary('RETR hello.py', r.write)
exec(r.getvalue())
这是单行代码(不需要模块!):
exec(open('hello.py','r').read())
我想 运行 Python 脚本位于服务器上,使用另一个 Python 脚本和 FTP。
下面是我想要 运行.
的 (hello.py
) 文件位置
from ftplib import FTP
ftp = FTP('host')
ftp.login(user='user', passwd = 'password')
print("successfully connected")
print ("File List:")
files = ftp.dir()
print (files)
您可以使用 system() 调用:
system('python hello.py')
注意:使用 hello.py 脚本的完整路径。
另一种方法是使用 subprocess 模块。
最好使用runpy模块
import runpy
runpy.run_path("code.py")
有关详细信息,请参阅文档页面runpy module documentation
你不能 运行 FTP 服务器上的任何东西,无论如何。
看到一个类似的问题:Run a script via FTP connection from PowerShell
是关于PowerShell的,不是Python,不过没关系
您将需要另一个访问权限。类似于 SSH shell 访问。
如果您真的想从 FTP 服务器下载脚本并 运行 在本地机器上使用:
with open('hello.py', 'wb') as f
ftp.retrbinary('RETR hello.py', f.write)
system('python hello.py')
如果您不想将脚本存储到本地文件,这也可以:
r = StringIO()
ftp.retrbinary('RETR hello.py', r.write)
exec(r.getvalue())
这是单行代码(不需要模块!):
exec(open('hello.py','r').read())