python套接字流重定向实例汇总
作者:hebedich 时间:2022-04-15 07:53:41
将套接字流重定向到标准输入或输出流
#!/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()
标签:python,套接字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用VB编写ActiveX DLL实现ASP编程
2008-10-21 21:28:00
用SQL统计SQLServe表存储空间大小的代码
2012-06-06 19:52:22
asp 批量删除选中的多条记录
2011-03-29 10:33:00
![](https://img.aspxhome.com/file/UploadPic/20112/5/201125112326166.gif)
MySQL数据库线程缓冲池详解
2012-04-13 11:48:01
一篇文章弄懂Python关键字、标识符和变量
2022-11-01 17:44:42
![](https://img.aspxhome.com/file/2023/4/66294_0s.png)
asp 判断上传文件中是否存在危险代码
2011-03-17 11:19:00
SQL Server日志清除的两种方法教程简介
2008-05-04 20:59:00
经验:解决DB2中出现的SQL1032N错误现象
2009-09-02 14:10:00
在 CSS 中关于字体处理效果的思考
2008-04-25 22:57:00
![](https://img.aspxhome.com/file/UploadPic/20071/2007131103237363s.gif)
asp如何自动更新导航栏?
2010-07-07 12:10:00
有用的:nth-child秘方
2011-07-01 12:56:11
jquery效率探索
2008-01-08 18:06:00
将滚动条(scrollbar)保持在最底部的方法
2008-02-21 10:05:00
asp事务处理的另外一个方法
2010-05-27 12:18:00
CSS网页布局扩展小技巧
2010-06-03 12:13:00
js实现滑动进度条效果
2023-08-24 03:47:39
![](https://img.aspxhome.com/file/2023/7/56257_0s.gif)
Asp用分页符实现长文章分页
2007-10-19 18:35:00
用什么视角做产品
2009-08-18 12:17:00
二级下拉菜单代码
2008-11-01 18:18:00
Iinternet Explorer浏览器简介(IE)
2009-02-05 20:59:00