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-03-11用Python實現一個模仿UP主彈幕控制的直播間功能
- 2022-03-11Python實戰之設計一個多功能辦公小工具
- 2022-03-11Python數據分析之缺失值檢測與處理詳解
- 2022-03-11Python變量的作用域詳解
- 2022-03-11Python之捕捉異常詳解
- 2022-03-11Python進度條可視化之監測程序運行速度
- Python
python直接訪問私有屬性的簡單方法
下面小編就為大家帶來一篇python直接訪問私有屬性的簡單方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧 ...
- Python
Python實現ping指定IP的示例
今天小編就為大家分享一篇Python實現ping指定IP的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...
- Python
使用NumPy和pandas對CSV文件進行寫操作的實例
今天小編就為大家分享一篇使用NumPy和pandas對CSV文件進行寫操作的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...
- Python
Python3以GitHub為例來實現模擬登錄和爬取的實例講解
在本篇內容里小編給大家分享的是關于Python3以GitHub為例來實現模擬登錄和爬取的實例講解,需要的朋友們可以參考下。 ...
- Python
python 插入Null值數據到Postgresql的操作
這篇文章主要介紹了python 插入Null值數據到Postgresql的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧...
- Python
在Windows系統上搭建Nginx+Python+MySQL環境的教程
這篇文章主要介紹了在Windows系統上搭建Nginx+Python+MySQL環境的教程,文中使用flup中間件及FastCGI方式連接,需要的朋友可以參考下 ...
- Python
Python的dict字典結構操作方法學習筆記
這篇文章主要介紹了Python的dict字典結構操作方法學習筆記本,字典的操作是Python入門學習中的基礎知識,需要的朋友可以參考下...
- Python
python 列表轉為字典的兩個小方法(小結)
這篇文章主要介紹了python 列表轉為字典的兩個小方法(小結),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的...