-1

I have a working php script that actually intercepts the incoming e-mail from sendmail and saves it to a file.

here it is:

<?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);

?>

i have never seen something like this in terms of reading from

 php://stdin

is there a PYTHON version of this ?

i rather use python than php.

but this php script works fine.

1
  • In a WSGI web application, you would not want to read from stdin. Not sure why you are tagging this with WSGI tags. Commented Jul 25, 2013 at 4:21

3 Answers 3

2

sys.stdin.read() should do it

1

The three standard I/O streams in Python are stored in sys.stdin, sys.stdout, and sys.stderr. They normally never need to be opened, only used.

foo = sys.stdin.read(1024)
1
  • ... without that 1024 for the example as it is only a chunk size when reading in the loop. I.e. the loop need not to be used in Python. One should be aware it is rather special way of using stdin. Generally, closing a stdin makes the same sense as closing the keyboard ;) The same way, waiting for eof from the keyboard does not make too much sense. Commented Jul 25, 2013 at 7:35
0

The whole script in Python:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.