python套接字流重定向实例汇总
将套接字流重定向到标准输入或输出流
#!/usr/bin/envpython3 """ 测试socket-stream重定向模式 """ importsys,os,time frommultiprocessingimportProcess fromsocketimport* definitListenerSocket(port=50008,host=''): """ 初始化在服务器模式下调用者用于监听连接的套接字 """ sock=socket() try: sock.bind((host,port)) exceptOSErrorase: print('Addressalreadyinuse') os._exit(1) sock.listen(5) conn,addr=sock.accept() returnconn defredirecOut(port=50008,host='localhost'): """ 在接受之前其他连接都失败,连接调用者标准输出流 到一个套接字,这个套接字用于gui监听,在收听者启动后,启动调用者 """ sock=socket() try: sock.connect((host,port)) exceptConnectionRefusedErrorase: print('connectionrefuse') os._exit(1) file=sock.makefile('w') sys.stdout=file returnsock defredirecIn(port=50008,host='localhost'): """ 连接调用者标准输入流到用于gui来提供的套接字 """ sock=socket() try: sock.connect((host,port)) exceptConnectionRefusedErrorase: print('conenctionrefuse') os._exit(1) file=sock.makefile('r') sys.stdin=file returnsock defredirecBothAsClient(port=50008,host='localhost'): """ 在这种模式下,连接调用者标准输入和输出流到相同的套接字 调用者对于服务器来说就是客户端:发送消息,接受响应答复 """ sock=socket() try: sock.connect((host,port)) exceptConnectionRefusedErrorase: print('connectionrefuse') os._exit(1) ofile=sock.makefile('w') ifile=sock.makefile('r') sys.stdout=ofile sys.stdin=ifile returnsock defredirecBothAsServer(port=50008,host='localhost'): """ 在这种模式下,连接调用者标准输入和输出流到相同的套接字,调用者对于 服务器来说就是服务端:接受消息,发送响应答复 """ sock=socket() try: sock.bind((host,port)) exceptOSErrorase: print('Addressalreadyinuse') os._exit(1) sock.listen(5) conn,addr=sock.accept() ofile=conn.makefile('w') ifile=conn.makefile('r') sys.stdout=ofile sys.stdin=ifile returnconn defserver1(): mypid=os.getpid() conn=initListenerSocket() file=conn.makefile('r') foriinrange(3): data=file.readline().rstrip() print('server%sgot[%s]'%(mypid,data)) defclient1(): time.sleep(1) mypid=os.getpid() redirecOut() foriinrange(3): print('client:%s:%s'%(mypid,i)) sys.stdout.flush() defserver2(): mypid=os.getpid() conn=initListenerSocket() foriinrange(3): conn.send(('server%sgot[%s]\n'%(mypid,i)).encode()) defclient2(): time.sleep(1) mypid=os.getpid() redirecIn() foriinrange(3): data=input() print('client%sgot[%s]]'%(mypid,data)) defserver3(): mypid=os.getpid() conn=initListenerSocket() file=conn.makefile('r') foriinrange(3): data=file.readline().rstrip() conn.send(('server%sgot[%s]\n'%(mypid,data)).encode()) defclient3(): time.sleep(1) mypid=os.getpid() redirecBothAsClient() foriinrange(3): print('Client%s:%s'%(mypid,data)) data=input() sys.stderr.write('client%sgot[%s]\n'%(mypid,data)) defserver4(port=50008,host='localhost'): mypid=os.getpid() sock=socket() try: sock.connect((host,port)) ConnectionRefusedErrorase: print('connectionrefuse') os._exit(1) file=sock.makefile('r') foriinrange(3): sock.send(('server%s:%S\n'%(mypid,i)).encode()) data=file.readline().rstrip() print('server%sgot[%s]'%(mypid,data)) defclient4(): time.sleep(1) mypid=os.getpid() redirecBothAsServer() foriinrange(3): data=input() print('client%sgot[%s]'%(mypid,data)) sys.stdout.flush() defserver5(): mypid=os.getpid() conn=initListenerSocket() file=conn.makefile('r') foriinrange(3): conn.send(('server%s:%s\n'%(mypid,i)).encode()) data=file.readline().rstrip() print('server%sgot[%s]'%(mypid,data)) defclient5(): mypid=os.getpid() s=redirecBothAsClient() foriinrange(3): data=input() print('client%sgot[%s]'%(mypid,data)) sys.stdout.flush() defmain(): server=eval('server'+sys.argv[1]) client=eval('client'+sys.argv[1]) Process(target=server).start() client() if__name__=='__main__': main()