#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11

    Question How do I us popen2 module?


    Well I have bin asking my self how to us the popen2 modules to open the cmd and to do something for example.

    Could you please help.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    You don't really need to open the command prompt to do something (and I don't think you could see it if you did run it from os.popen) - you can just do something and read the results directly;

    [code]
    import os
    source = os.popen("dir c:\\")
    print source.read()
    [/code

    To use popen2 to read and write... I don't know - what are you trying to run?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Python has a rich but confusing number of variations of popen. However if you install Python 2.4 they have a new 'subprocess' module that should simplify the code needed.

    However if you want to interact with the program instead of just run it and capture the output, then you will need to use some variation of expect - google for 'pexpect' for a pure python implementation.

    Dave - The Developers' Coach
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    As has been said - many ways to skin the cat but to answer your question ...
    Code:
    #example of popen2 picking up IP addresses
    import popen2
    procout,procin = popen2.popen2("ipconfig")
    response = procout.readlines()
    for n in response:
        if "IP Address" in n:
            print n
    Have fun

    grim
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    thanks man ill try it

    thanks it works but how would u make it send it to someone for example a email and could it also show u the host name
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    could someone tell me how i would get it the ip adress sent to me or whatever i get from that place like the platform and version to my email for example

IMN logo majestic logo threadwatch logo seochat tools logo