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

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

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

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

#!/usr/bin/env python3
"""
測試socket-stream 重定向模式
"""
import sys,os,time
from multiprocessing import Process
from socket import *
 
def initListenerSocket(port=50008,host=''):
    """ 
    初始化在服務器模式下調用者用于監(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'):
    """ 
    在接受之前其他連接都失敗,連接調用者標準輸出流
    到一個套接字,這個套接字用于gui監(jiān)聽,在收聽者啟動后,啟動調用者
    """
    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'):
    """
    在這種模式下,連接調用者標準輸入和輸出流到相同的套接字
    調用者對于服務器來說就是客戶端:發(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'):
    """
    在這種模式下,連接調用者標準輸入和輸出流到相同的套接字,調用者對于
    服務器來說就是服務端:接受消息,發(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()

相關文章

  • Python應用利器之緩存機制的妙用詳解

    Python應用利器之緩存機制的妙用詳解

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

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

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

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

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

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

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

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

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

    Python命令行中引導用戶指定選擇文檔示例

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

    Win下PyInstaller 安裝和使用教程

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

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

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

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

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

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

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

最新評論