-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_all.py
159 lines (113 loc) · 4.15 KB
/
test_all.py
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
import time
import pytest
from simple_socket.tcp_client import SimpleTCPClient, SimpleSSLClient
from simple_socket.tcp_server import SimpleTCPServer, SimpleSSLServer
import socket
def test_SimpleTCPClientAndServer():
server = SimpleTCPServer(1885, trace=True)
serverReceivedConnection = False
serverReceivedDisconnection = False
def ServerConnection(c, state):
print('ServerConnection(', c, state)
nonlocal serverReceivedConnection
nonlocal serverReceivedDisconnection
if state == 'Connected':
serverReceivedConnection = True
elif state == 'Disconnected':
serverReceivedDisconnection = True
print('serverReceivedConnection=', serverReceivedConnection)
print('serverReceivedDisconnection=', serverReceivedDisconnection)
server.onConnected = ServerConnection
server.onDisconnected = ServerConnection
def ServerRx(c, data):
c.Send(b'echo ' + data) # echo the data
server.onReceive = ServerRx
client = SimpleTCPClient(socket.gethostname(), 1885, trace=True)
clientRecievedEcho = False
def ClientRx(_, data):
print('ClientRx(', data)
nonlocal clientRecievedEcho
if b'echo test' == data:
clientRecievedEcho = True
client.onReceive = ClientRx
def ClientConnection(_, state):
print('ClientConnection(', state)
client.onConnected = ClientConnection
client.onDisconnected = ClientConnection
while client.ConnectionStatus == 'Disconnected' or serverReceivedConnection:
print('Waiting for connection')
time.sleep(1)
time.sleep(1)
client.Send('test')
time.sleep(1)
assert serverReceivedConnection
assert clientRecievedEcho
client.Disconnect()
time.sleep(1)
assert serverReceivedDisconnection
server.Stop()
client.Stop()
def test_SSLServerClient():
server = SimpleSSLServer(3888, trace=True)
serverReceivedConnection = False
serverReceivedDisconnection = False
def ServerConnection(c, state):
print('ServerConnection(', c, state)
nonlocal serverReceivedConnection
nonlocal serverReceivedDisconnection
if state == 'Connected':
serverReceivedConnection = True
elif state == 'Disconnected':
serverReceivedDisconnection = True
print('serverReceivedConnection=', serverReceivedConnection)
print('serverReceivedDisconnection=', serverReceivedDisconnection)
server.onConnected = ServerConnection
server.onDisconnected = ServerConnection
def ServerRx(c, data):
c.Send(b'echo ' + data) # echo the data
server.onReceive = ServerRx
client = SimpleSSLClient(socket.gethostname(), 3888, autoConnect=False, trace=True)
print('client.Hostname=', client.Hostname)
clientRecievedEcho = False
def ClientRx(_, data):
print('ClientRx(', data)
nonlocal clientRecievedEcho
if b'echo test' == data:
clientRecievedEcho = True
client.onReceive = ClientRx
def ClientConnection(_, state):
print('ClientConnection(', state)
client.onConnected = ClientConnection
client.onDisconnected = ClientConnection
res = client.Connect()
print('res=', res)
if 'Disconnected' in res:
raise ConnectionError()
time.sleep(1)
client.Send('test')
time.sleep(1)
assert serverReceivedConnection
assert clientRecievedEcho
client.Disconnect()
time.sleep(1)
assert serverReceivedDisconnection
server.Stop()
client.Stop()
def test_SMD():
client = SimpleTCPClient('192.168.68.143', 23)
client.onConnected = lambda _, state: print('SMD:', state)
client.onDisconnected = lambda _, state: print('SMD:', state)
def HandleRX(intf, data):
print('HandleRX(', data)
if 'Password:' in data.decode():
print('sending password *****')
intf.Send('extron\r')
client.onReceive = HandleRX
while 'Disconnected' in client.ConnectionStatus:
print('waiting for connection')
time.sleep(1)
for i in range(3):
time.sleep(1)
msg = 'q'
print('Send(', msg)
client.Send(msg)