Simulate socket with files or stdin/stdout

Donn Cave donn at u.washington.edu
Wed Mar 31 20:11:21 EST 2004


In article <c4fenn$o9h$1 at newshispeed.ch>,
 "Jean-Pierre Bergamin" <james at ractive.ch> wrote:

> We are forced to use a quite old simulation software that's based on
> Modula-2. The idea is now to let this software "talk" to the outside world
> over a TCP/IP network.
> 
> Since the program has no possibility to use sockets or other mechanisms to
> send data over the network we have the idea to let python do the network
> part and let the simu software communicate with the python script in some
> other way:
> 
> One idea is to use stdout and stdin (but I'm not even sure if stdout/in are
> available in the simulation software).
> 
> +------------+  stdout     stdin  +------------+  socket.write
> |            |------------------->|  python-   |----------------
> | Simulation |                    |  script    |
> |            |<-------------------|            |<---------------
> +------------+  stdin      stdout +------------+  socket.read


On UNIX (and MacOS X), it may be easier than this already.
If your application will support the above configuration,
then it will almost for sure talk directly to the socket.
Try this:

import posix
import socket

def bind(port):
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.bind(('', port))
   s.listen(1)
   c, a = s.accept()
   print 'connection from', a
   s.close()
   return c

s = bind(2004)
fd = s.fileno()
posix.dup2(fd, 0)
posix.dup2(fd, 1)
posix.close(fd)
posix.execve('/usr/local/bin/similator', ['similator', '-flag1'],
   posix.environ)


I've used posix.this and posix.that, instead of the politically
correct os.this and os.that, to emphasize the fact that this
is not portable to Windows.

If the above works, you'll be able to telnet directly to the
application host, port 2004, and interact with the application.
This obviously needs to be made a little more secure, but as
long as it's just authentication and authorization, that can
happen prior to the execve().

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list