问题描述
我想在 python 中使用 rlcone.如果我不使用密码加密我的 rclone 配置,那么我可以简单地这样做:
I would like to use rlcone inside python.If I don't encrypt my rclone config with a password, then I can simply do this:
import subprocess
process = subprocess.Popen(["rclone", "listremotes"], shell=True, stdin=subprocess.PIPE)
output = process.communicate()
print(output)
但我想用密码保护我的 Rclone 配置,所以我需要一种方法将它发送到 rclone.我跟着这个answer但我收到错误无法读取密码:句柄无效
:
But I want to protect my Rclone config with a password, so I need a way to send it to rclone. I followed this answer but I get the error Failed to read password: The handle is invalid
:
import subprocess
psw= input("enter psw")
psw_byte= str.encode(psw+'\n')
process = subprocess.Popen(["rclone", "listremotes"], shell=True, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.stdin.write(psw_byte)
process.stdin.flush()
output = process.communicate()
print(output)
推荐答案
我建议您使用 RCLONE_CONFIG_PASS
环境变量将配置密码传递给 rclone.请参阅有关配置加密的文档中的部分.
I recommend you use the RCLONE_CONFIG_PASS
environment variable to pass the config password into rclone. See the section in the docs about configuration encryption.
例如
os.environ("RCLONE_CONFIG_PASS") = "mypassword"
除非您指定一个 tty,否则读取密码的 Unix 程序往往无法像这样编写脚本.
Unix programs reading passwords tend not to be scriptable like this unless you assign a tty.
虽然这很麻烦,所以我建议只设置环境变量.
That is quite a lot of trouble though, so I recommend just setting the environment variable instead.
PS 如果你只想为子进程设置环境,你可以这样做:带有修改环境的 Python 子进程/Popen
PS If you want to just set the enviroment for the subprocess you can do it like this: Python subprocess/Popen with a modified environment
PPS 我是 rclone 的作者 :-)
PPS I'm the author of rclone :-)
这篇关于Python子进程:如何发送密码(rclone配置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!