redirecting Stdin

Jon Arter nospam at nospam.yes
Wed Dec 3 11:45:15 CET 2003

assume  we have a file :
after redirection   sys.stdin = file('newinput')

#newinput file 

import socket

def detectip():
    buf = socket.gethostname()
    remotehost = socket.gethostbyname(buf)
    return remotehost

if __name__ == "__main__":

    print "Your IP address is", detectip()


exec line

This results in a parsing error when the file reading reaches the
 def detectip(): 
sentence . 
You can execute the command line as long as it is a single line

How to Get arround this ?

With regards 

On Wed, 03 Dec 2003 01:54:11 -0800, Erik Max Francis <max at>

>Jon Arter wrote:
>> Is is Possible to redirect the Python stdin in order to receive stdin
>> commands from a file ?
>> The time of redirection can  last until exiting the Python  and should
>> be transparent for python it self.
>	sys.stdin = file('newinput')

More information about the Python-list mailing list