如何创建此PHP脚本的PYTHON版本。(摘自php://stdin )

如何创建此PHP脚本的PYTHON版本。(摘自php://stdin ),python,Python,我有一个可以工作的php脚本,它可以截获来自sendmail的传入电子邮件并将其保存到一个文件中 这是: <?php $fd = fopen("php://stdin", "r"); while (!feof($fd)) { $email .= fread($fd, 1024); } fclose($fd); $fdw = fopen("/test/mail.txt", "w+"); fwrite($fdw, $email); fclose($fdw); ?> 有PYTH

我有一个可以工作的php脚本,它可以截获来自sendmail的传入电子邮件并将其保存到一个文件中

这是:

<?php
$fd = fopen("php://stdin", "r");
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);
$fdw = fopen("/test/mail.txt", "w+");
fwrite($fdw, $email);
fclose($fdw);

?>
有PYTHON版本的吗

我宁愿使用python而不是php


但是这个php脚本工作得很好。

sys.stdin.read()应该可以做到这一点

Python中的三个标准I/O流存储在
sys.stdin
sys.stdout
sys.stderr
中。它们通常不需要打开,只需要使用

foo = sys.stdin.read(1024)

Python中的整个脚本:

import sys
with open('/test/mail.txt', 'w+') as f:
    f.write(sys.stdin.read())

在WSGI web应用程序中,您不希望从stdin读取数据。不知道为什么要用WSGI标签来标记它。。。。在本例中不使用该
1024
,因为在循环中读取时它只是块大小。也就是说,循环不需要在Python中使用。人们应该意识到这是一种非常特殊的使用标准输入法的方式。通常,关闭
stdin
与关闭键盘具有相同的意义;)同样,从键盘等待eof也没有太多意义。
import sys
with open('/test/mail.txt', 'w+') as f:
    f.write(sys.stdin.read())