0

I have 3 stream I want to connect:

  • TCP stream from socket (input, output)
  • stdin, stdout, stderr
  • subprocess stream (p.stdin, p.stderr, p.stdout)

And this is how I want to wire them together:

(p is process, TCP is socket connection)

  • TCP.out -> stdin, p.stdin
  • p.stdour -> stdout, TCP.in
  • p.stderr -> stderr

I've been digging through documentation and googling around, however to no avail. Can you please point me in the right direction how would I do this?

I know I can treat every stream as file descriptor and read/write to it... but it feels ugly and that better solution exists, either already supported or some external library.

nmiculinic
  • 2,224
  • 3
  • 24
  • 39
  • what does it mean `TCP.in -> stdin` in terms of sockets and running processes? Also, `p.stdin -> stdout` doesn't make sense -- why do you want to write something to a child only to echo it via parent's stdout instead of writing to the stdout directly? Here's a [code example that connects a socket and `subprocess`' stdin/stdout (on Unix)](http://stackoverflow.com/questions/19961052/what-is-the-difference-if-i-dont-use-stdout-subprocess-pipe-in-subprocess-popen/19961290#comment29713500_19961290) – jfs Nov 01 '15 at 18:33
  • I've noticed my mistake and corrected wiring scheme. On the left are output stream which I want to duplicate/pipe to 1 or more input streams. – nmiculinic Nov 01 '15 at 22:02
  • `TCP.out -> stdin` also doesn't make sense. Why do you need to write to your own stdin from within the process itself? Do you want to capture the socket output (for some processing) and to pass it to the child process too (like `tee` utility)? Here's [code example that uses threads to (tee) duplicate the streams](http://stackoverflow.com/q/4984428/4279). Here's [`asyncio` version](http://stackoverflow.com/a/25960956/4279). – jfs Nov 01 '15 at 22:16
  • fix your diagram: objects on the left should be readable (`.read()`, `.recv()` methods), objects on the right should be writable (`.write()`, `send()`). Mention whether objects's `.fileno()` return valid file descriptors. What is your OS (does `select` work on pipes on your system)? – jfs Nov 01 '15 at 22:59

0 Answers0