1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#!/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套接字流重定向实例汇总
2020-08-15 12:03脚本之家 Python
套接字是一种具有之前所说的“通信端点”概念的计算网络数据结构。相当于电话插口,没它无法通信,这个比喻非常形象。今天我们就来汇总一下套接字流重定向的实例
延伸 · 阅读
- 2022-06-28Python数据读写之Python读写CSV文件
- 2022-06-28Python操作HDF5文件示例
- 2022-06-28python操作csv格式文件之csv.DictReader()方法
- 2022-06-28Python简明讲解filter函数的用法
- 2022-06-28分享13个非常有用的Python代码片段
- 2022-06-28使用Docker制作Python环境连接Oracle镜像
- Python
python中使用smtplib和email模块发送邮件实例
python脚本发邮件,一般会用到smtplib和email这两个模块。看看该模块怎么使用,先看smtplib模块。 smtplib模块定义了一个简单的SMTP客户端,可以用来在互联网上...
- Python
Django自带的用户验证系统实现
这篇文章主要介绍了Django自带的用户验证系统实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...
- Python
Python实现敏感词过滤的4种方法
这篇文章主要介绍了Python实现敏感词过滤的4种方法,帮助大家处理不和谐的言论,感兴趣的朋友可以了解下...
- Python
分析并输出Python代码依赖的库的实现代码
这篇文章主要介绍了分析并输出Python代码依赖的库的实现代码,需要的朋友可以参考下...
- Python
Windows下Python的Django框架环境部署及应用编写入门
这篇文章主要介绍了Windows下Python的Django框架环境部署及程序编写入门,Django在Python的框架中算是一个重量级的MVC框架,本文将从程序部署开始讲到hellow world...
- Python
python访问类中docstring注释的实现方法
这篇文章主要介绍了python访问类中docstring注释的实现方法,涉及python类注释的访问技巧,非常具有实用价值,需要的朋友可以参考下...
- Python
pandas将DataFrame的列变成行索引的方法
下面小编就为大家分享一篇pandas将DataFrame的列变成行索引的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...
- Python
keras.layer.input()用法说明
这篇文章主要介绍了keras.layer.input()用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...