将套接字流重定向到标准输入或输出流
#!/usr/bin/env python3"""测试socket-stream 重定向模式"""import sys,os,timefrom multiprocessing import Processfrom socket import * def initListenerSocket(port=50008,host=''): """ 初始化在服务器模式下调用者用于监听连接的套接字 """ sock=socket() try: sock.bind((host,port)) except 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'): """ 在接受之前其他连接都失败,连接调用者标准输出流 到一个套接字,这个套接字用于gui监听,在收听者启动后,启动调用者 """ sock=socket() try: sock.connect((host,port)) except ConnectionRefusedError as e: print('connection refuse') os._exit(1) file=sock.makefile('w') sys.stdout=file return sock def redirecIn(port=50008,host='localhost'): """ 连接调用者标准输入流到用于gui来提供的套接字 """ sock=socket() try: sock.connect((host,port)) except ConnectionRefusedError as e: print('conenction refuse') os._exit(1) file=sock.makefile('r') sys.stdin=file return sock def redirecBothAsClIEnt(port=50008,host='localhost'): """ 在这种模式下,连接调用者标准输入和输出流到相同的套接字 调用者对于服务器来说就是客户端:发送消息,接受响应答复 """ sock=socket() try: sock.connect((host,port)) except 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'): """ 在这种模式下,连接调用者标准输入和输出流到相同的套接字,调用者对于 服务器来说就是服务端:接受消息,发送响应答复 """ sock=socket() try: sock.bind((host,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()总结
以上是内存溢出为你收集整理的python套接字流重定向实例汇总全部内容,希望文章能够帮你解决python套接字流重定向实例汇总所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)