国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python套接字流重定向實例匯總

2019-11-25 16:54:06
字體:
來源:轉載
供稿:網友

將套接字流重定向到標準輸入或輸出流

#!/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,port))    except 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()

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥化市| 湘西| 和顺县| 平山县| 霍林郭勒市| 临邑县| 奎屯市| 子洲县| 琼结县| 芜湖市| 英山县| 右玉县| 昌吉市| 黎城县| 麻栗坡县| 宝应县| 天全县| 黄龙县| 和田市| 龙南县| 全南县| 中西区| 太原市| 大厂| 仲巴县| 治多县| 茂名市| 淳安县| 玉龙| 增城市| 永和县| 新化县| 前郭尔| 辰溪县| 乌兰县| 广水市| 江津市| 梅河口市| 铜梁县| 邮箱| 达州市|