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)文章
利用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)向PPT中插入表格與圖片的方法詳解
這篇文章將帶大家學(xué)習(xí)一下如何在PPT中插入表格與圖片以及在表格中插入內(nèi)容,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-05-05
使用matplotlib創(chuàng)建Gif動圖的實現(xiàn)
本文主要介紹了使用matplotlib創(chuàng)建Gif動圖的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

