日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python重定向到socket_python套接字流重定向实例汇总

發(fā)布時間:2023/12/3 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python重定向到socket_python套接字流重定向实例汇总 小編覺得挺不錯的,現(xià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)用者標準輸出流

到一個套接字,這個套接字用于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)用者標準輸入流到用于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)用者標準輸入和輸出流到相同的套接字

調(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)用者標準輸入和輸出流到相同的套接字,調(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()

總結(jié)

以上是生活随笔為你收集整理的python重定向到socket_python套接字流重定向实例汇总的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。