-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMP
More file actions
executable file
·149 lines (127 loc) · 3.74 KB
/
MP
File metadata and controls
executable file
·149 lines (127 loc) · 3.74 KB
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
#!/usr/bin/env python3
import socket
import signal
import sys
import selectors
import time
#TCP_IP = '192.168.120.238'
TCP_IP = "127.0.0.1"
TCP_PORT = 8000
BUFFER_SIZE = 1024
#MESSAGE = "Hello, World!"
TCP_IP_OUT = '127.0.0.1'
TCP_PORT_OUT_1 = 8001
TCP_PORT_OUT_2 = 8002
port = 1
in_s = out_s1 = out_s2 = conn1 = conn2 = None
def signal_handler(signal, frame):
if in_s != None: in_s.close()
if conn1 != None: conn1.close()
if out_s1 != None: out_s1.close()
if conn2 != None: conn2.close()
if out_s2 != None: out_s2.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def read_in(conn, mask):
global port
data = conn.recv(BUFFER_SIZE)
for sym in bytes(data):
if (sym >= 127):
port = sym - 126
elif port == 1:
if conn1:
conn1.send(bytes([sym]))
else:
print(chr(sym), end='')
elif port == 2:
if conn2:
conn2.send(bytes([sym]))
def read1(conn, mask):
data1 = conn.recv(BUFFER_SIZE)
if data1:
for sym in bytes(data1):
in_s.send(bytes([127]))
in_s.send(bytes([sym]))
time.sleep(0.05)
print(bytes([sym]))
else:
print('closing', conn)
sel.unregister(conn)
conn.close()
conn1 = None
def read2(conn, mask):
data2 = conn.recv(BUFFER_SIZE)
if data2:
for sym in bytes(data2):
in_s.send(bytes([128]))
in_s.send(bytes([sym]))
print(chr(sym), end='')
print()
else:
print('closing', conn)
sel.unregister(conn)
conn.close()
conn2 = None
#~ time.sleep(0.00001)
def my_read():
in_s.send(bytes([128]))
in_s.send(b'a')
time.sleep(3)
#~ for i in range(2):
#~ in_s.send(bytes([128]))
#~ in_s.send(b'$')
#~ for i in range(1):
#~ for sym in range(127):
#~ if sym != 35:
#~ in_s.send(bytes([128]))
#~ in_s.send(bytes([sym]))
#~ #time.sleep(0.01)
#~ print(sym)
#~ in_s.send(bytes([128]))
#~ in_s.send(b'#')
#~ in_s.send(bytes([128]))
#~ in_s.send(b'a')
#~ in_s.send(bytes([128]))
#~ in_s.send(b'0')
def accept1(sock, mask):
global conn1
conn1, addr = sock.accept() # Should be ready
print('accepted', conn1, 'from', addr)
conn1.setblocking(False)
sel.register(conn1, selectors.EVENT_READ, read1)
#~ my_read()
def accept2(sock, mask):
global conn2
conn2, addr = sock.accept() # Should be ready
print('accepted', conn2, 'from', addr)
conn2.setblocking(False)
sel.register(conn2, selectors.EVENT_READ, read2)
my_read()
#~ finally:
#~ in_s.close()
#~ conn1.close()
#~ out_s1.close()
#~ conn2.close()
#~ out_s2.close()
in_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
in_s.connect((TCP_IP, TCP_PORT))
in_s.setblocking(False)
sel = selectors.DefaultSelector()
sel.register(in_s, selectors.EVENT_READ, read_in)
out_s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
out_s1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
out_s1.bind((TCP_IP_OUT, TCP_PORT_OUT_1))
out_s1.listen(10)
out_s1.setblocking(False)
sel.register(out_s1, selectors.EVENT_READ, accept1)
out_s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
out_s2.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
out_s2.bind((TCP_IP_OUT, TCP_PORT_OUT_2))
out_s2.listen(10)
out_s2.setblocking(False)
sel.register(out_s2, selectors.EVENT_READ, accept2)
while True:
events = sel.select()
for key, mask in events:
callback = key.data
callback(key.fileobj, mask)