sock socket(af_inetsock_streamsock connect((hostport)file sock makefile(' 'sys stdout file return sock caller operates in client mode file interfacetextbuffered make prints go to sock send if caller needs to access it raw def redirectin(port=porthost=host)""connect caller' standard input stream to socket for gui to provide ""sock socket(af_inetsock_streamsock connect((hostport)file sock makefile(' 'file interface wrapper sys stdin file make input come from sock recv return sock return value can be ignored def redirectbothasclient(port=porthost=host)""connect caller' standard input and output stream to same socket in this modecaller is client to serversends msgreceives reply ""sock socket(af_inetsock_streamsock connect((hostport)or open in 'rwmode ofile sock makefile(' 'file interfacetextbuffered ifile sock makefile(' 'two file objects wrap same socket sys stdout ofile make prints go to sock send sys stdin ifile make input come from sock recv return sock def redirectbothasserver(port=porthost=host)""connect caller' standard input and output stream to same socket in this modecaller is server to clientreceives msgsend reply ""sock socket(af_inetsock_streamsock bind((hostport)caller is listener here sock listen( connaddr sock accept(ofile conn makefile(' 'file interface wrapper ifile conn makefile(' 'two file objects wrap same socket sys stdout ofile make prints go to sock send sys stdin ifile make input come from sock recv return conn to testthe script in example - defines five sets of client/server functions it runs the client' code in processbut deploys the python multiprocessing module we met in to portably spawn the server function' side of the dialog in separate process in the endthe client and server test functions run in different processesbut converse over socket that is connected to standard streams within the test script' process making sockets look like files and streams