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

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

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

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

#!/usr/bin/env python3
"""
測試socket-stream 重定向模式
"""
import sys,os,time
from multiprocessing import Process
from socket import *
 
def initListenerSocket(port=50008,host=''):
    """ 
    初始化在服務(wù)器模式下調(diào)用者用于監(jiān)聽連接的套接字
    """
    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)聽,在收聽者啟動后,啟動調(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來提供的套接字
    """
    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)用者對于服務(wù)器來說就是客戶端:發(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)用者對于
    服務(wù)器來說就是服務(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)自動化的sql延時(shí)注入

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

    這篇文章主要為大家詳細(xì)介紹了如何基于python實(shí)現(xiàn)自動化的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),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • python中使用矢量化替換循環(huán)詳解

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

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

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

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

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

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

    python pywinauto使用過程及問題小結(jié)

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

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

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

    Python讀寫unicode文件的方法

    這篇文章主要介紹了Python讀寫unicode文件的方法,涉及Python針對文件的讀取及編碼操作的相關(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如何使用雙線性插值計(jì)算網(wǎng)格內(nèi)數(shù)據(jù)

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

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

最新評論

惠东县| 陇川县| 海淀区| 荆州市| 潜山县| 巴林左旗| 嘉义县| 伊通| 遂昌县| 长沙市| 武安市| 茂名市| 砀山县| 武平县| 南皮县| 仲巴县| 黑河市| 阿巴嘎旗| 沛县| 澳门| 武强县| 鞍山市| 铅山县| 麻江县| 望奎县| 多伦县| 武乡县| 百色市| 仙游县| 沈丘县| 城步| 平邑县| 沾化县| 兴隆县| 贵州省| 吉隆县| 平遥县| 凯里市| 清水河县| 固阳县| 法库县|