A socket is a computing network data structure that has the previously mentioned Concept of "Communication endpoint. It is equivalent to a telephone plug-in and cannot communicate without it. this metaphor is very vivid. Today, we will summarize the socket stream redirection instances to redirect the socket stream to the standard input or output stream.
#! /Usr/bin/env python3 "test socket-stream redirection mode" import sys, OS, timefrom multiprocessing import Processfrom socket import * def initListenerSocket (port = 50008, host = ''):" initialize the socket used by the caller to listen for connection in server mode "" sock = socket () try: sock. bind (host, port) handle T OSError as e: print ('address already in use') OS. _ exit (1) sock. listen (5) conn, addr = sock. accept () return conn def redirecOut (port = 50008, host = 'localhost, this socket is used for gui listening. After the listener starts, the caller "sock = socket () try: sock. connect (host, port) does t ConnectionRefusedError as e: print ('Connection refuse') OS. _ exit (1) file = sock. makefile ('W') sys. stdout = file return sock def redirecIn (port = 50008, host = 'localhost '): "Connect the caller's standard input stream to the socket provided by the gui" sock = socket () try: sock. connect (host, port) does t ConnectionRefusedError as e: print ('conenction refuse') OS. _ exit (1) file = sock. makefile ('R') sys. stdin = file return sock def redirecBothAsClient (port = 50008, host = 'localhost'): "In this mode, connect the caller standard input and output streams to the same socket caller for the server is the client: send a message, accept the response "" sock = socket () try: sock. connect (host, port) does t ConnectionRefusedError as e: print ('Connection refuse') OS. _ exit (1) ofile = sock. makefile ('W') ifile = sock. makefile ('R') sys. stdout = ofile sys. stdin = ifile return sock def redirecBothAsServer (port = 50008, host = 'localhost'): "In this mode, connect the caller's standard input and output streams to the same socket, the caller is the server for the server: receives the message and sends a response "" sock = socket () try: sock. bind (host, port) handle T OSError as e: print ('address already in use') OS. _ exit (1) sock. listen (5) conn, addr = sock. accept () ofile = conn. makefile ('W') ifile = conn. makefile ('R') sys. stdout = ofile sys. stdin = ifile return conn def server1 (): mypid = OS. getpid () conn = initListenerSocket () file = conn. makefile ('R') for I in range (3): data = file. readline (). rstrip () print ('server % s got [% s] '% (mypid, data) def client1 (): time. sleep (1) mypid = OS. getpid () redirecOut () for I in range (3): print ('client: % s: % s' % (mypid, I) sys. stdout. flush () def server2 (): mypid = OS. getpid () conn = initListenerSocket () for I in range (3): conn. send ('server % s got [% s] \ n' % (mypid, I )). encode () def client2 (): time. sleep (1) mypid = OS. getpid () redirecIn () for I in range (3): data = input () print ('client % s got [% s] '% (mypid, data )) def server3 (): mypid = OS. getpid () conn = initListenerSocket () file = conn. makefile ('R') for I in range (3): data = file. readline (). rstrip () conn. send ('server % s got [% s] \ n' % (mypid, data )). encode () def client3 (): time. sleep (1) mypid = OS. getpid () redirecBothAsClient () for I in range (3): print ('client % s: % s' % (mypid, data) data = input () sys. stderr. write ('client % s got [% s] \ n' % (mypid, data) def server4 (port = 50008, host = 'localhost'): mypid = OS. getpid () sock = socket () try: sock. connect (host, port) ConnectionRefusedError as e: print ('Connection refuse') OS. _ exit (1) file = sock. makefile ('R') for I in range (3): sock. send ('server % s: % S \ n' % (mypid, I )). encode () data = file. readline (). rstrip () print ('server % s got [% s] '% (mypid, data) def client4 (): time. sleep (1) mypid = OS. getpid () redirecBothAsServer () for I in range (3): data = input () print ('client % s got [% s] '% (mypid, data) sys. stdout. flush () def server5 (): mypid = OS. getpid () conn = initListenerSocket () file = conn. makefile ('R') for I in range (3): conn. send ('server % s: % s \ n' % (mypid, I )). encode () data = file. readline (). rstrip () print ('server % s got [% s] '% (mypid, data) def client5 (): mypid = OS. getpid () s = redirecBothAsClient () for I in range (3): data = input () print ('client % s got [% s] '% (mypid, data )) sys. stdout. flush () def main (): server = eval ('server' + sys. argv [1]) client = eval ('client' + sys. argv [1]) Process (target = server ). start () client () if _ name __= = '_ main _': main ()