stdin, stdout, redmon

Discussion in 'Python' started by Bernard Desnoues, Jan 21, 2008.

  1. Hi,

    I've got a problem with the use of Redmon (redirection port monitor). I
    intend to develop a virtual printer so that I can modify data sent to
    the printer.
    Redmon send the data flow to the standard input and lauchs the Python
    program which send modified data to the standard output (Windows XP and
    Python 2.5 context).
    I can manipulate the standard output.

    "import sys
    sys.stdout.write(data)"

    it works.
    But how to manipulate standard input so that I can store data in a
    string or in an object file ? There's no "read" method.

    "a = sys.stdin.read()" doesn't work.
    "f = open(sys.stdin)" doesn't work.

    I don't find anything in the documentation. How to do that ?
    Thanks in advance.

    Bernard Desnoues
    Librarian
    Bibliothèque de géographie - Sorbonne
    Bernard Desnoues, Jan 21, 2008
    #1
    1. Advertising

  2. According to various tutorials this should work.

    <code>
    |import sys
    data = sys.stdin.readlines()
    print "Counted", len(data), "lines."|
    </code>

    Please use google before asking such questions. This was found with only
    one search for the terms 'python read stdin'

    Rolf

    Bernard Desnoues wrote:
    > Hi,
    >
    > I've got a problem with the use of Redmon (redirection port monitor). I
    > intend to develop a virtual printer so that I can modify data sent to
    > the printer.
    > Redmon send the data flow to the standard input and lauchs the Python
    > program which send modified data to the standard output (Windows XP and
    > Python 2.5 context).
    > I can manipulate the standard output.
    >
    > "import sys
    > sys.stdout.write(data)"
    >
    > it works.
    > But how to manipulate standard input so that I can store data in a
    > string or in an object file ? There's no "read" method.
    >
    > "a = sys.stdin.read()" doesn't work.
    > "f = open(sys.stdin)" doesn't work.
    >
    > I don't find anything in the documentation. How to do that ?
    > Thanks in advance.
    >
    > Bernard Desnoues
    > Librarian
    > Bibliothèque de géographie - Sorbonne
    Rolf van de Krol, Jan 21, 2008
    #2
    1. Advertising

  3. Rolf van de Krol a écrit :
    > According to various tutorials this should work.
    >
    > <code>
    > |import sys
    > data = sys.stdin.readlines()
    > print "Counted", len(data), "lines."|
    > </code>
    >
    > Please use google before asking such questions. This was found with only
    > one search for the terms 'python read stdin'
    >
    > Rolf
    >
    > Bernard Desnoues wrote:
    >> Hi,
    >>
    >> I've got a problem with the use of Redmon (redirection port monitor).
    >> I intend to develop a virtual printer so that I can modify data sent
    >> to the printer.
    >> Redmon send the data flow to the standard input and lauchs the Python
    >> program which send modified data to the standard output (Windows XP
    >> and Python 2.5 context).
    >> I can manipulate the standard output.
    >>
    >> "import sys
    >> sys.stdout.write(data)"
    >>
    >> it works.
    >> But how to manipulate standard input so that I can store data in a
    >> string or in an object file ? There's no "read" method.
    >>
    >> "a = sys.stdin.read()" doesn't work.
    >> "f = open(sys.stdin)" doesn't work.
    >>
    >> I don't find anything in the documentation. How to do that ?
    >> Thanks in advance.
    >>
    >> Bernard Desnoues
    >> Librarian
    >> Bibliothèque de géographie - Sorbonne


    Hello Rolf,

    I know this code because I have search a solution !
    Your google code doesn't work ! No attribute "readlines".

    >>> import sys
    >>> data = sys.stdin.readlines()


    Traceback (most recent call last):
    File "<pyshell#1>", line 1, in <module>
    data = sys.stdin.readlines()
    AttributeError: readlines
    Bernard Desnoues, Jan 21, 2008
    #3
  4. I don't know what you did with your Python installation, but for me this
    works perfectly.

    test3.py contains:
    <code>
    import sys

    print sys.stdin.readlines()
    </code>

    test.txt contains:
    <code>
    Testline1
    Testline2
    </code>

    Output of 'python test3.py < test.txt' is:
    <code>
    ['Testline1\n', 'Testline2']
    </code>

    Just plain simple and just works.

    Rolf



    Bernard Desnoues wrote:
    > Rolf van de Krol a écrit :
    >
    >> According to various tutorials this should work.
    >>
    >> <code>
    >> |import sys
    >> data = sys.stdin.readlines()
    >> print "Counted", len(data), "lines."|
    >> </code>
    >>
    >> Please use google before asking such questions. This was found with only
    >> one search for the terms 'python read stdin'
    >>
    >> Rolf
    >>
    >> Bernard Desnoues wrote:
    >>
    >>> Hi,
    >>>
    >>> I've got a problem with the use of Redmon (redirection port monitor).
    >>> I intend to develop a virtual printer so that I can modify data sent
    >>> to the printer.
    >>> Redmon send the data flow to the standard input and lauchs the Python
    >>> program which send modified data to the standard output (Windows XP
    >>> and Python 2.5 context).
    >>> I can manipulate the standard output.
    >>>
    >>> "import sys
    >>> sys.stdout.write(data)"
    >>>
    >>> it works.
    >>> But how to manipulate standard input so that I can store data in a
    >>> string or in an object file ? There's no "read" method.
    >>>
    >>> "a = sys.stdin.read()" doesn't work.
    >>> "f = open(sys.stdin)" doesn't work.
    >>>
    >>> I don't find anything in the documentation. How to do that ?
    >>> Thanks in advance.
    >>>
    >>> Bernard Desnoues
    >>> Librarian
    >>> Bibliothèque de géographie - Sorbonne
    >>>

    >
    > Hello Rolf,
    >
    > I know this code because I have search a solution !
    > Your google code doesn't work ! No attribute "readlines".
    >
    > >>> import sys
    > >>> data = sys.stdin.readlines()

    >
    > Traceback (most recent call last):
    > File "<pyshell#1>", line 1, in <module>
    > data = sys.stdin.readlines()
    > AttributeError: readlines
    >
    Rolf van de Krol, Jan 21, 2008
    #4
  5. Hello,

    I checked under linux and it works :
    text.txt :
    "first line of the text file
    second line of the text file"

    test.py :
    "import sys
    a = sys.stdin.readlines()
    x = ''.join(a)
    x = x.upper()
    sys.stdout.write(x)"

    >cat text.txt | python test.py


    But I reinstalled Python 2.5 under Windows XP and it doesn't work
    anyway. Can you confirm that your script works with Win XP and Python 2.5 ?

    Regards

    Rolf van de Krol a écrit :
    > I don't know what you did with your Python installation, but for me this
    > works perfectly.
    >
    > test3.py contains:
    > <code>
    > import sys
    >
    > print sys.stdin.readlines()
    > </code>
    >
    > test.txt contains:
    > <code>
    > Testline1
    > Testline2
    > </code>
    >
    > Output of 'python test3.py < test.txt' is:
    > <code>
    > ['Testline1\n', 'Testline2']
    > </code>
    >
    > Just plain simple and just works.
    >
    > Rolf
    >
    >
    >
    > Bernard Desnoues wrote:
    >> Rolf van de Krol a écrit :
    >>
    >>> According to various tutorials this should work.
    >>>
    >>> <code>
    >>> |import sys
    >>> data = sys.stdin.readlines()
    >>> print "Counted", len(data), "lines."|
    >>> </code>
    >>>
    >>> Please use google before asking such questions. This was found with
    >>> only one search for the terms 'python read stdin'
    >>>
    >>> Rolf
    >>>
    >>> Bernard Desnoues wrote:
    >>>
    >>>> Hi,
    >>>>
    >>>> I've got a problem with the use of Redmon (redirection port
    >>>> monitor). I intend to develop a virtual printer so that I can modify
    >>>> data sent to the printer.
    >>>> Redmon send the data flow to the standard input and lauchs the
    >>>> Python program which send modified data to the standard output
    >>>> (Windows XP and Python 2.5 context).
    >>>> I can manipulate the standard output.
    >>>>
    >>>> "import sys
    >>>> sys.stdout.write(data)"
    >>>>
    >>>> it works.
    >>>> But how to manipulate standard input so that I can store data in a
    >>>> string or in an object file ? There's no "read" method.
    >>>>
    >>>> "a = sys.stdin.read()" doesn't work.
    >>>> "f = open(sys.stdin)" doesn't work.
    >>>>
    >>>> I don't find anything in the documentation. How to do that ?
    >>>> Thanks in advance.
    >>>>
    >>>> Bernard Desnoues
    >>>> Librarian
    >>>> Bibliothèque de géographie - Sorbonne
    >>>>

    >>
    >> Hello Rolf,
    >>
    >> I know this code because I have search a solution !
    >> Your google code doesn't work ! No attribute "readlines".
    >>
    >> >>> import sys
    >> >>> data = sys.stdin.readlines()

    >>
    >> Traceback (most recent call last):
    >> File "<pyshell#1>", line 1, in <module>
    >> data = sys.stdin.readlines()
    >> AttributeError: readlines
    Bernard Desnoues, Jan 22, 2008
    #5
  6. Bernard Desnoues

    Tim Golden Guest

    Bernard Desnoues wrote:
    > Hello,
    >
    > I checked under linux and it works :
    > text.txt :
    > "first line of the text file
    > second line of the text file"
    >
    > test.py :
    > "import sys
    > a = sys.stdin.readlines()
    > x = ''.join(a)
    > x = x.upper()
    > sys.stdout.write(x)"
    >
    > >cat text.txt | python test.py

    >
    > But I reinstalled Python 2.5 under Windows XP and it doesn't work
    > anyway. Can you confirm that your script works with Win XP and Python 2.5 ?


    How are you invoking the script under WinXP? If you're
    using the standard file associations then stdin/stdout
    won't work correctly. However, they produce a specific
    error message:

    <dump>
    C:\temp>type test3.py
    import sys

    print sys.stdin.readlines ()
    C:\temp>
    C:\temp>type test3.py | test3.py
    Traceback (most recent call last):
    File "C:\temp\test3.py", line 3, in <module>
    print sys.stdin.readlines ()
    IOError: [Errno 9] Bad file descriptor

    C:\temp>type test3.py | python test3.py
    ['import sys\n', '\n', 'print sys.stdin.readlines ()']

    </dump>

    TJG
    Tim Golden, Jan 22, 2008
    #6
  7. Bernard Desnoues

    John Machin Guest

    On Jan 22, 8:42 pm, Bernard Desnoues <>
    wrote:
    > Hello,
    >
    > I checked under linux and it works :
    > text.txt :
    > "first line of the text file
    > second line of the text file"
    >
    > test.py :
    > "import sys
    > a = sys.stdin.readlines()
    > x = ''.join(a)
    > x = x.upper()
    > sys.stdout.write(x)"
    >
    > >cat text.txt | python test.py

    >
    > But I reinstalled Python 2.5 under Windows XP and it doesn't work
    > anyway. Can you confirm that your script works with Win XP and Python 2.5 ?
    >
    > Regards
    >
    > Rolf van de Krol a écrit :
    >
    > > I don't know what you did with your Python installation, but for me this
    > > works perfectly.

    >
    > > test3.py contains:
    > > <code>
    > > import sys

    >
    > > print sys.stdin.readlines()
    > > </code>

    >
    > > test.txt contains:
    > > <code>
    > > Testline1
    > > Testline2
    > > </code>

    >
    > > Output of 'python test3.py < test.txt' is:
    > > <code>
    > > ['Testline1\n', 'Testline2']
    > > </code>

    >
    > > Just plain simple and just works.

    >
    > > Rolf

    >
    > > Bernard Desnoues wrote:
    > >> Rolf van de Krol a écrit :

    >
    > >>> According to various tutorials this should work.

    >
    > >>> <code>
    > >>> |import sys
    > >>> data = sys.stdin.readlines()
    > >>> print "Counted", len(data), "lines."|
    > >>> </code>

    >
    > >>> Please use google before asking such questions. This was found with
    > >>> only one search for the terms 'python read stdin'

    >
    > >>> Rolf

    >
    > >>> Bernard Desnoues wrote:

    >
    > >>>> Hi,

    >
    > >>>> I've got a problem with the use of Redmon (redirection port
    > >>>> monitor). I intend to develop a virtual printer so that I can modify
    > >>>> data sent to the printer.
    > >>>> Redmon send the data flow to the standard input and lauchs the
    > >>>> Python program which send modified data to the standard output
    > >>>> (Windows XP and Python 2.5 context).
    > >>>> I can manipulate the standard output.

    >
    > >>>> "import sys
    > >>>> sys.stdout.write(data)"

    >
    > >>>> it works.
    > >>>> But how to manipulate standard input so that I can store data in a
    > >>>> string or in an object file ? There's no "read" method.

    >
    > >>>> "a = sys.stdin.read()" doesn't work.
    > >>>> "f = open(sys.stdin)" doesn't work.

    >
    > >>>> I don't find anything in the documentation. How to do that ?
    > >>>> Thanks in advance.

    >
    > >>>> Bernard Desnoues
    > >>>> Librarian
    > >>>> Bibliothèque de géographie - Sorbonne

    >
    > >> Hello Rolf,

    >
    > >> I know this code because I have search a solution !
    > >> Your google code doesn't work ! No attribute "readlines".

    >
    > >> >>> import sys
    > >> >>> data = sys.stdin.readlines()

    >
    > >> Traceback (most recent call last):
    > >> File "<pyshell#1>", line 1, in <module>
    > >> data = sys.stdin.readlines()
    > >> AttributeError: readlines


    Excuse me, gentlemen, may I be your referee *before* you resort to
    pistols at dawn?

    ===== IDLE =====
    IDLE 1.2.1
    >>> import sys
    >>> sys.stdin.readlines


    Traceback (most recent call last):
    File "<pyshell#1>", line 1, in <module>
    sys.stdin.readlines
    AttributeError: readlines
    >>>


    ===== Command Prompt =====
    C:\junk>python
    Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
    (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.stdin.readlines

    <built-in method readlines of file object at 0x00B1F020>
    >>>


    HTH,
    John
    John Machin, Jan 22, 2008
    #7
  8. Well, that's at least weird. I did test my code with Python 2.5 on Win
    XP, using the command prompt. But testing it with IDLE gives exactly the
    same error Bernard has. So apparently STDIN can't be accessed with IDLE.

    Rolf

    John Machin wrote:
    >
    > Excuse me, gentlemen, may I be your referee *before* you resort to
    > pistols at dawn?
    >
    > ===== IDLE =====
    > IDLE 1.2.1
    >
    >>>> import sys
    >>>> sys.stdin.readlines
    >>>>

    >
    > Traceback (most recent call last):
    > File "<pyshell#1>", line 1, in <module>
    > sys.stdin.readlines
    > AttributeError: readlines
    >
    >
    > ===== Command Prompt =====
    > C:\junk>python
    > Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
    > (Intel)] on win32
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    >>>> import sys
    >>>> sys.stdin.readlines
    >>>>

    > <built-in method readlines of file object at 0x00B1F020>
    >
    >
    > HTH,
    > John
    >
    Rolf van de Krol, Jan 22, 2008
    #8
  9. Hi,

    This is Windows bug that is described here: http://support.microsoft.com/default.aspx?kbid=321788

    This article also contains solution: you need to add registry value:

    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies
    \Explorer
    InheritConsoleHandles = 1 (REG_DWORD type)

    Do not forget to launch new console (cmd.exe) after editing registry.

    Alternatively you can use following command

    cat file | python script.py

    instead of

    cat file | python script.py

    Regards,
    Konstantin

    On Jan 22, 1:02 pm, Rolf van de Krol <> wrote:
    > Well, that's at least weird. I did test my code with Python 2.5 on Win
    > XP, using the command prompt. But testing it with IDLE gives exactly the
    > same error Bernard has. So apparently STDIN can't be accessed with IDLE.
    >
    > Rolf
    >
    > John Machin wrote:
    >
    > > Excuse me, gentlemen, may I be your referee *before* you resort to
    > > pistols at dawn?

    >
    > > ===== IDLE =====
    > > IDLE 1.2.1

    >
    > >>>> import sys
    > >>>> sys.stdin.readlines

    >
    > > Traceback (most recent call last):
    > > File "<pyshell#1>", line 1, in <module>
    > > sys.stdin.readlines
    > > AttributeError: readlines

    >
    > > ===== Command Prompt =====
    > > C:\junk>python
    > > Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
    > > (Intel)] on win32
    > > Type "help", "copyright", "credits" or "license" for more information.

    >
    > >>>> import sys
    > >>>> sys.stdin.readlines

    >
    > > <built-in method readlines of file object at 0x00B1F020>

    >
    > > HTH,
    > > John
    Konstantin Shaposhnikov, Jan 22, 2008
    #9
  10. Sorry, I meant:

    Alternatively you can use following command

    cat file | python script.py

    instead of

    cat file | script.py




    On Jan 22, 1:54 pm, Konstantin Shaposhnikov <>
    wrote:
    > Hi,
    >
    > This is Windows bug that is described here:http://support.microsoft.com/default.aspx?kbid=321788
    >
    > This article also contains solution: you need to add registry value:
    >
    > HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies
    > \Explorer
    > InheritConsoleHandles = 1 (REG_DWORD type)
    >
    > Do not forget to launch new console (cmd.exe) after editing registry.
    >
    > Alternatively you can use following command
    >
    > cat file | python script.py
    >
    > instead of
    >
    > cat file | python script.py
    >
    > Regards,
    > Konstantin
    >
    > On Jan 22, 1:02 pm, Rolf van de Krol <> wrote:
    >
    > > Well, that's at least weird. I did test my code with Python 2.5 on Win
    > > XP, using the command prompt. But testing it with IDLE gives exactly the
    > > same error Bernard has. So apparently STDIN can't be accessed with IDLE.

    >
    > > Rolf

    >
    > > John Machin wrote:

    >
    > > > Excuse me, gentlemen, may I be your referee *before* you resort to
    > > > pistols at dawn?

    >
    > > > ===== IDLE =====
    > > > IDLE 1.2.1

    >
    > > >>>> import sys
    > > >>>> sys.stdin.readlines

    >
    > > > Traceback (most recent call last):
    > > > File "<pyshell#1>", line 1, in <module>
    > > > sys.stdin.readlines
    > > > AttributeError: readlines

    >
    > > > ===== Command Prompt =====
    > > > C:\junk>python
    > > > Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
    > > > (Intel)] on win32
    > > > Type "help", "copyright", "credits" or "license" for more information.

    >
    > > >>>> import sys
    > > >>>> sys.stdin.readlines

    >
    > > > <built-in method readlines of file object at 0x00B1F020>

    >
    > > > HTH,
    > > > John
    Konstantin Shaposhnikov, Jan 22, 2008
    #10
  11. Bernard Desnoues

    Thynnus Guest

    On 1/22/2008 8:54 AM, Konstantin Shaposhnikov wrote:
    > Hi,
    >
    > This is Windows bug that is described here: http://support.microsoft.com/default.aspx?kbid=321788
    >
    > This article also contains solution: you need to add registry value:
    >
    > HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies
    > \Explorer
    > InheritConsoleHandles = 1 (REG_DWORD type)
    >
    > Do not forget to launch new console (cmd.exe) after editing registry.
    >
    > Alternatively you can use following command
    >
    > cat file | python script.py
    >
    > instead of
    >
    > cat file | python script.py
    >
    > Regards,
    > Konstantin


    Nice one, Konstantin!

    I can confirm that adding the registry key solves the problem on XPsp2:

    -----After adding InheritConsoleHandles DWORD 1 key-----
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    D:\temp>type test3.py | test3.py
    ['import sys\n', '\n', 'print sys.stdin.readlines ()\n']

    D:\temp>

    The KB article is quite poorly written. Even though it seems to state that
    issue was 'solved for win2k with sp4, for XP with sp1', and gives no indication
    that the key is needed after the sp's are applied *even though* it is in fact
    necessary to the solution.

    Questions:
    -Any side effects to look out for?
    -If the change is relatively benign, should it be part of the install?
    -Is this worth a documentation patch? If yes to where, and I'll give it a
    shot.

    -Thynnus
    Thynnus, Jan 22, 2008
    #11
  12. Bernard Desnoues

    Thynnus Guest

    On 1/21/2008 9:02 AM, Bernard Desnoues wrote:
    > Hi,
    >
    > I've got a problem with the use of Redmon (redirection port monitor). I
    > intend to develop a virtual printer so that I can modify data sent to
    > the printer.


    FWIW: there is a nice update the RedMon (v1.7) called RedMon EE (v1.81)
    available at http://www.is-foehr.com/ that I have used and like a lot.

    From the developers website:
    Fixed issues and features [with respect to the orininal RedMon]
    * On Windows Terminal Server or Windows XP with fast user switching, the
    "Prompt for filename" dialog will appear on the current session.
    * "SaveAs" now shows XP style dialogs if running under XP
    * Support for PDF Security added - experimental -.
    * Support for setting the task priority - experimental -
    * Use of file-shares as output
    * Environment variables are passed to the AfterWorks Process now.
    * Environment variables are replaced in the program arguments. No
    workaround is needed.
    * RedMon EE comes with an RPC communication feature which could transfer
    output-files back to the client starting the print job on a print server.
    Error messages will be send to the client.
    * Redmon EE may start a process after the print job has finished (After
    works process).
    e.g. starting a presentation program to show the pdf generated by
    GhostScript.
    * additional debug messages may be written for error analysis.
    No special debug version is needed.
    * user interface has been rewritten. May be it's more friendly.
    Added some basic system information which may help if running in failures.
    * new feature: running on a print server.
    * cleanup of documentnames "Microsoft -"
    * define templates for output-file names with full environment variable
    substitution
    e.g. %homedrive%\%homedir%\%redmon-user%-%date%-%time%-%n.pdf
    * RedMon EE does not support for NT 3.5 and Windows 95/98 !

    -Thynnus
    Thynnus, Jan 22, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,950
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    784
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,341
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    424
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    289
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page