最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python套接字流重定向?qū)嵗齾R總

 更新時(shí)間:2016年03月03日 08:48:41   投稿:hebedich  
套接字是一種具有之前所說(shuō)的“通信端點(diǎn)”概念的計(jì)算網(wǎng)絡(luò)數(shù)據(jù)結(jié)構(gòu)。相當(dāng)于電話(huà)插口,沒(méi)它無(wú)法通信,這個(gè)比喻非常形象。今天我們就來(lái)匯總一下套接字流重定向的實(shí)例

將套接字流重定向到標(biāo)準(zhǔn)輸入或輸出流

#!/usr/bin/env python3
"""
測(cè)試socket-stream 重定向模式
"""
import sys,os,time
from multiprocessing import Process
from socket import *
 
def initListenerSocket(port=50008,host=''):
    """ 
    初始化在服務(wù)器模式下調(diào)用者用于監(jiān)聽(tīng)連接的套接字
    """
    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'):
    """ 
    在接受之前其他連接都失敗,連接調(diào)用者標(biāo)準(zhǔn)輸出流
    到一個(gè)套接字,這個(gè)套接字用于gui監(jiān)聽(tīng),在收聽(tīng)者啟動(dòng)后,啟動(dòng)調(diào)用者
    """
    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'):
    """ 
    連接調(diào)用者標(biāo)準(zhǔn)輸入流到用于gui來(lái)提供的套接字
    """
    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'):
    """
    在這種模式下,連接調(diào)用者標(biāo)準(zhǔn)輸入和輸出流到相同的套接字
    調(diào)用者對(duì)于服務(wù)器來(lái)說(shuō)就是客戶(hù)端:發(fā)送消息,接受響應(yīng)答復(fù)
    """
    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'):
    """
    在這種模式下,連接調(diào)用者標(biāo)準(zhǔn)輸入和輸出流到相同的套接字,調(diào)用者對(duì)于
    服務(wù)器來(lái)說(shuō)就是服務(wù)端:接受消息,發(fā)送響應(yīng)答復(fù)
    """
    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()

相關(guān)文章

  • python實(shí)現(xiàn)自動(dòng)化的sql延時(shí)注入

    python實(shí)現(xiàn)自動(dòng)化的sql延時(shí)注入

    這篇文章主要為大家詳細(xì)介紹了如何基于python實(shí)現(xiàn)自動(dòng)化的sql延時(shí)注入腳本,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-12-12
  • Pandas缺失值填充 df.fillna()的實(shí)現(xiàn)

    Pandas缺失值填充 df.fillna()的實(shí)現(xiàn)

    本文主要介紹了Pandas缺失值填充 df.fillna()的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • python中使用矢量化替換循環(huán)詳解

    python中使用矢量化替換循環(huán)詳解

    矢量化是在數(shù)據(jù)集上實(shí)現(xiàn) (NumPy) 數(shù)組操作的技術(shù)。在后臺(tái),它將操作一次性應(yīng)用于數(shù)組或系列的所有元素(不同于一次操作一行的“for”循環(huán))。
    2023-01-01
  • 教你如何用python開(kāi)發(fā)一款數(shù)字推盤(pán)小游戲

    教你如何用python開(kāi)發(fā)一款數(shù)字推盤(pán)小游戲

    這篇文章主要介紹了教你如何用python開(kāi)發(fā)一款數(shù)字推盤(pán)小游戲,文中有非常詳細(xì)的代碼示例,喜對(duì)歡玩小游戲的或者正在學(xué)習(xí)python小游戲開(kāi)發(fā)的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-04-04
  • matplotlib bar()實(shí)現(xiàn)百分比堆積柱狀圖

    matplotlib bar()實(shí)現(xiàn)百分比堆積柱狀圖

    這篇文章主要介紹了matplotlib bar()實(shí)現(xiàn)百分比堆積柱狀圖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • python pywinauto使用過(guò)程及問(wèn)題小結(jié)

    python pywinauto使用過(guò)程及問(wèn)題小結(jié)

    在pywinauto庫(kù)中,uia即UIAutomation,是微軟提供的用于用戶(hù)界面自動(dòng)化測(cè)試和輔助功能訪(fǎng)問(wèn)的技術(shù)框架,UIAutomation支持自動(dòng)化腳本與各種UI元素交互,本文給大家介紹python pywinauto使用過(guò)程及問(wèn)題小結(jié),感興趣的朋友一起看看吧
    2024-10-10
  • Python+Pygame實(shí)現(xiàn)神廟逃亡游戲

    Python+Pygame實(shí)現(xiàn)神廟逃亡游戲

    這篇文章主要為大家介紹了如何利用Python和Pygame動(dòng)畫(huà)制作一個(gè)神廟逃亡類(lèi)似的小游戲。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下
    2022-05-05
  • Python讀寫(xiě)unicode文件的方法

    Python讀寫(xiě)unicode文件的方法

    這篇文章主要介紹了Python讀寫(xiě)unicode文件的方法,涉及Python針對(duì)文件的讀取及編碼操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • python機(jī)器學(xué)習(xí)基礎(chǔ)特征工程算法詳解

    python機(jī)器學(xué)習(xí)基礎(chǔ)特征工程算法詳解

    這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)基礎(chǔ)特征工程的算法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2021-11-11
  • python如何使用雙線(xiàn)性插值計(jì)算網(wǎng)格內(nèi)數(shù)據(jù)

    python如何使用雙線(xiàn)性插值計(jì)算網(wǎng)格內(nèi)數(shù)據(jù)

    這篇文章主要介紹了python如何使用雙線(xiàn)性插值計(jì)算網(wǎng)格內(nèi)數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評(píng)論

洛浦县| 改则县| 台前县| 翁牛特旗| 弋阳县| 佛山市| 黄冈市| 乐平市| 嫩江县| 施甸县| 溆浦县| 沧源| 金乡县| 开江县| 荆州市| 霞浦县| 丘北县| 通化市| 通州市| 泰安市| 安达市| 莎车县| 武宣县| 香格里拉县| 阆中市| 祁连县| 霸州市| 枝江市| 阿城市| 永州市| 涟水县| 卓资县| 青海省| 封开县| 武清区| 贵南县| 合江县| 兰州市| 安化县| 青海省| 白城市|