python套接字流重定向实例汇总
admin
2023-07-31 02:31:15
0

将套接字流重定向到标准输入或输出流

#!/usr/bin/env python3
\"\"\"
测试socket-stream 重定向模式
\"\"\"
import sys,os,time
from multiprocessing import Process
from socket import *
 
def initListenerSocket(port=50008,host=\'\'):
    \"\"\" 
    初始化在服务器模式下调用者用于监听连接的套接字
    \"\"\"
    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监听,在收听者启动后,启动调用者
    \"\"\"
    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\'):
    \"\"\"
    在这种模式下,连接调用者标准输入和输出流到相同的套接字
    调用者对于服务器来说就是客户端:发送消息,接受响应答复
    \"\"\"
    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\'):
    \"\"\"
    在这种模式下,连接调用者标准输入和输出流到相同的套接字,调用者对于
    服务器来说就是服务端:接受消息,发送响应答复
    \"\"\"
    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()

相关内容

热门资讯

500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
python查找阿姆斯特朗数 题目解释 如果一个n位正整数等于其各位数字的n次方之和,则称该数为阿姆斯特朗数。 例如1^3 + 5...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...