在 Python 中创建临时 FIFO(命名管道)?
如何在 Python 中创建临时 FIFO(命名管道)?这应该有效:
import tempfile
temp_file_name = mktemp()
os.mkfifo(temp_file_name)
open(temp_file_name, os.O_WRONLY)
# ... some process, somewhere, will read it ...
但是,由于 Python Docs 11.6 中的大警告,我很犹豫以及可能被删除,因为它已被弃用。
编辑:值得注意的是,我尝试过tempfile.NamedTemporaryFile
(以及扩展名tempfile.mkstemp
),但是os.mkfifo< /代码> 抛出:
操作系统错误-17:文件已存在
OSError -17:当您在 mkstemp/NamedTemporaryFile 创建的文件上运行该文件时,
How can you create a temporary FIFO (named pipe) in Python? This should work:
import tempfile
temp_file_name = mktemp()
os.mkfifo(temp_file_name)
open(temp_file_name, os.O_WRONLY)
# ... some process, somewhere, will read it ...
However, I'm hesitant because of the big warning in Python Docs 11.6 and potential removal because it's deprecated.
EDIT: It's noteworthy that I've tried tempfile.NamedTemporaryFile
(and by extension tempfile.mkstemp
), but os.mkfifo
throws:
OSError -17: File already exists
when you run it on the files that mkstemp/NamedTemporaryFile have created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果文件已经存在,
os.mkfifo()
将失败,并出现异常OSError: [Errno 17] File isn't
,因此这里不存在安全问题。使用 tempfile.mktemp() 的安全问题是竞争条件,攻击者有可能在您自己打开文件之前创建一个同名的文件,但由于 os.mkfifo如果文件已经存在,() 就会失败,这不是问题。但是,由于
mktemp()
已被弃用,因此您不应使用它。您可以使用tempfile.mkdtemp()
代替:编辑:我应该明确指出,仅仅因为
mktemp()
漏洞由此避免,仍然存在其他漏洞需要考虑的常见安全问题;例如,攻击者可以在您的程序之前创建 fifo(如果他们有适当的权限),如果错误/异常处理不当,可能会导致您的程序崩溃。os.mkfifo()
will fail with exceptionOSError: [Errno 17] File exists
if the file already exists, so there is no security issue here. The security issue with usingtempfile.mktemp()
is the race condition where it is possible for an attacker to create a file with the same name before you open it yourself, but sinceos.mkfifo()
fails if the file already exists this is not a problem.However, since
mktemp()
is deprecated you shouldn't use it. You can usetempfile.mkdtemp()
instead:EDIT: I should make it clear that, just because the
mktemp()
vulnerability is averted by this, there are still the other usual security issues that need to be considered; e.g. an attacker could create the fifo (if they had suitable permissions) before your program did which could cause your program to crash if errors/exceptions are not properly handled.您可能会发现使用以下上下文管理器很方便,它可以为您创建和删除临时文件:
您可以像这样使用它:
You may find it handy to use the following context manager, which creates and removes the temporary file for you:
You can use it, for example, like this:
怎么样使用
How about using
如果它是在您的程序中使用,而不是与任何外部程序一起使用,请查看 队列模块< /a>.作为一个额外的好处,Python 队列是线程安全的。
If it's for use within your program, and not with any externals, have a look at the Queue module. As an added benefit, python queues are thread-safe.
实际上,
mkstemp
所做的就是在循环中运行mktemp
并不断尝试独占创建,直到成功(请参阅 stdlib 源代码此处)。您可以使用 os.mkfifo 执行相同的操作:Effectively, all that
mkstemp
does is runmktemp
in a loop and keeps attempting to exclusively create until it succeeds (see stdlib source code here). You can do the same withos.mkfifo
:为什么不直接使用 mkstemp() ?
例如:
Why not just use mkstemp()?
For example: