在 Python 中以不同用户身份写入文件
我正在尝试将字节写入文件。一般来说,人们可以简单地这样做:
...
fb = file.read() # this contains the bytes to copy
with open("/tmp/", "wb") as out:
out.write(fb)
它工作得很好,但我试图让它与 subprocess
一起工作。为什么?因为我需要在不同用户的所有权下创建输出文件 - 比如说 userA
。我看到的唯一可能的方法是用户模拟。下面的例子:
# runs as root
import subprocess
def run(cmd, **kwargs):
popen = subprocess.Popen(
cmd.split(),
stdout=kwargs.pop("stdout", subprocess.PIPE),
stderr=kwargs.pop("stderr", subprocess.PIPE),
user=kwargs.get("user")
)
popen.communicate()
dst = "/data/userA" # special (network) location that only userA can access (not even root)
byte_data = file.read() # this contains the bytes to copy
user = "userA"
with open(dst, mode="wb") as out_f:
cmd = f"echo -n {byte_data}"
run(cmd=cmd, user=user, stdout=out_f)
如果我发送一个 txt
文件,我的内容是 b"text content"
。
我的限制:
- 文件可以是任何格式:文本、图像、视频等,因此我无法解码它。
- 我无法以
root
身份创建该文件并将其chown
给userA
,因为即使 root 也无法访问该位置。
I am trying to write bytes to a file. Generally, one could simply do:
...
fb = file.read() # this contains the bytes to copy
with open("/tmp/", "wb") as out:
out.write(fb)
It works perfectly, but I am trying to make it work with subprocess
. Why? Because I need the output file to be created under the ownership of a different user - say userA
. And the only way possible I am seeing is user impersonation. An example below:
# runs as root
import subprocess
def run(cmd, **kwargs):
popen = subprocess.Popen(
cmd.split(),
stdout=kwargs.pop("stdout", subprocess.PIPE),
stderr=kwargs.pop("stderr", subprocess.PIPE),
user=kwargs.get("user")
)
popen.communicate()
dst = "/data/userA" # special (network) location that only userA can access (not even root)
byte_data = file.read() # this contains the bytes to copy
user = "userA"
with open(dst, mode="wb") as out_f:
cmd = f"echo -n {byte_data}"
run(cmd=cmd, user=user, stdout=out_f)
If I send a txt
file, my content is b"text content"
.
My limitations:
- File can be of any format: text, images, video, etc. therefore I can't decode it.
- I can't create the file as
root
andchown
it touserA
since not even root can access that location.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 shell 中,
sudo tee
有时用于替换输出重定向,以确保正确的用户打开文件。你可以在 Python 中做同样的事情。请注意,
subprocess.Popen
本身可以直接从输入文件中读取;您不需要构建人工命令。本质上,这个
copy_to
只是subprocess.run
的包装器,它使用给定的输入文件、输出文件名称运行tee
em> 和用户。任何其他关键字参数都将直接传递给subprocess.run
。关键是
tee
打开输出文件进行写入,而不是 Python 脚本。In shell,
sudo tee
is sometimes used to replace output redirection to ensure that the correct user opens the file.You can do the same thing in Python. Note that
subprocess.Popen
itself can read directly from your input file; you don't need to construct an artificial command.Essentially, this
copy_to
is just a wrapper aroundsubprocess.run
that runstee
using the given input file, output file name, and user. Any other keyword arguments will be passed directly tosubprocess.run
.The key is that
tee
opens the output file for writing, not your Python script.