欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

將套接字流重定向到標(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)輸出流
    到一個套接字,這個套接字用于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應(yīng)用利器之緩存機(jī)制的妙用詳解

    Python應(yīng)用利器之緩存機(jī)制的妙用詳解

    在 Python 應(yīng)用程序中,使用緩存能夠顯著提高性能并降低資源消耗,本文將詳細(xì)介紹如何在 Python 中實現(xiàn)緩存機(jī)制,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • 淺談Python中的生成器和迭代器

    淺談Python中的生成器和迭代器

    這篇文章主要介紹了Python中的生成器和迭代器的的相關(guān)資料,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))

    利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))

    這篇文章主要給大家介紹了關(guān)于利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2018-03-03
  • Python實現(xiàn)的樸素貝葉斯分類器示例

    Python實現(xiàn)的樸素貝葉斯分類器示例

    這篇文章主要介紹了Python實現(xiàn)的樸素貝葉斯分類器,結(jié)合具體實例形式分析了基于Python實現(xiàn)的樸素貝葉斯分類器相關(guān)定義與使用技巧,需要的朋友可以參考下
    2018-01-01
  • Python編寫電話薄實現(xiàn)增刪改查功能

    Python編寫電話薄實現(xiàn)增刪改查功能

    這篇文章主要為大家詳細(xì)介紹了Python編寫電話薄實現(xiàn)增刪改查功能的相關(guān)資料,感興趣的朋友可以參考一下
    2016-05-05
  • Python命令行中引導(dǎo)用戶指定選擇文檔示例

    Python命令行中引導(dǎo)用戶指定選擇文檔示例

    這篇文章主要為大家介紹了Python命令行中引導(dǎo)用戶指定選擇文檔示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Win下PyInstaller 安裝和使用教程

    Win下PyInstaller 安裝和使用教程

    pyinstaller是一個非常簡單的打包python的py文件的庫,這篇文章主要介紹了PyInstaller-Win安裝和使用教程,本文通過流程實例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2019-12-12
  • Python實現(xiàn)向PPT中插入表格與圖片的方法詳解

    Python實現(xiàn)向PPT中插入表格與圖片的方法詳解

    這篇文章將帶大家學(xué)習(xí)一下如何在PPT中插入表格與圖片以及在表格中插入內(nèi)容,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-05-05
  • 使用matplotlib創(chuàng)建Gif動圖的實現(xiàn)

    使用matplotlib創(chuàng)建Gif動圖的實現(xiàn)

    本文主要介紹了使用matplotlib創(chuàng)建Gif動圖的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • python 七種郵件內(nèi)容發(fā)送方法實例

    python 七種郵件內(nèi)容發(fā)送方法實例

    這篇文章主要介紹了python 七種郵件內(nèi)容發(fā)送方法實例,需要的朋友可以參考下
    2014-04-04

最新評論