standard i/o with os.execl()

Donn Cave donn at drizzle.com
Wed Aug 28 03:43:46 EDT 2002


Quoth Brian Lee <senux at senux.com>:

| I linked standard input/output to a socket by these lines.
|
| sys.stdin  = conn.makefile('rb', -1)
| sys.stdout = conn.makefile('wb',  0)
| sys.stderr = sys.stdout
| print 'hello world'
|
| And it works: When I connect to my test server, I got 'hello
| world' string immediately. 
|
| But when I use follow line, 'hello world (test.py') string does
| not printed through socket, but it printed on terminal which I
| start my test server.
|
| os.execv('test.py', ('', ''))
|
| #!/usr/bin/env python
| # test.py 
| print 'hello world (test.py)'
|
| How can I fully link standard input/output to socket even in
| os.exec*() functions? Any advice?

I gather you have some kind of UNIX platform.  In this case, you
can use os.dup2, with the socket as a file descriptor.  That's
the same thing but at a much lower level - dup2 is an ioctl -
where you're working with the things that processes inherit.
I think your example in these terms would be something like

  fd = conn.fileno()
  os.dup2(fd, 0)
  os.dup2(fd, 1)
  print 'Hello, world'
  os.execv('test.py', ('test.py',))

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list