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)聽,在收聽者啟動(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來提供的套接字
"""
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()
您可能感興趣的文章:
- Python網(wǎng)絡(luò)編程之TCP與UDP協(xié)議套接字用法示例
- Python網(wǎng)絡(luò)編程 Python套接字編程
- Python中利用原始套接字進(jìn)行網(wǎng)絡(luò)編程的示例
- python socket網(wǎng)絡(luò)編程步驟詳解(socket套接字使用)
- python原始套接字編程示例分享
- 詳解python3中socket套接字的編碼問題解決
- Python里disconnect UDP套接字的方法
- python利用socketserver實(shí)現(xiàn)并發(fā)套接字功能
- Python網(wǎng)絡(luò)編程之TCP套接字簡單用法示例
相關(guān)文章
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),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
教你如何用python開發(fā)一款數(shù)字推盤小游戲
這篇文章主要介紹了教你如何用python開發(fā)一款數(shù)字推盤小游戲,文中有非常詳細(xì)的代碼示例,喜對歡玩小游戲的或者正在學(xué)習(xí)python小游戲開發(fā)的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04
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é)
在pywinauto庫中,uia即UIAutomation,是微軟提供的用于用戶界面自動(dòng)化測試和輔助功能訪問的技術(shù)框架,UIAutomation支持自動(dòng)化腳本與各種UI元素交互,本文給大家介紹python pywinauto使用過程及問題小結(jié),感興趣的朋友一起看看吧2024-10-10
Python+Pygame實(shí)現(xiàn)神廟逃亡游戲
這篇文章主要為大家介紹了如何利用Python和Pygame動(dòng)畫制作一個(gè)神廟逃亡類似的小游戲。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-05-05
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ù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

