Skip to content

Commit 8276e7a

Browse files
emaicusbmcutler
authored andcommitted
Use new knownhost files (#20)
* updated to use new knownhosts txt files. * use new knownhost files * fixed issue
1 parent 68181c0 commit 8276e7a

File tree

9 files changed

+156
-137
lines changed

9 files changed

+156
-137
lines changed

examples/16_docker_network_python/config/config.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"container_name" : "client",
3434
// It can be important to ensure your container's start in the correct order.
3535
// In this example, we want the server to start before the client, so we add a sleep command.
36-
"commands" : ["sleep 1", "python3 client.py client 0"],
36+
"commands" : ["sleep 2", "python3 client.py client 0"],
3737
"outgoing_connections" : ["server"]
3838
},
3939
{
@@ -74,7 +74,7 @@
7474
},
7575
{
7676
"container_name" : "client",
77-
"commands" : ["sleep 1", "python3 client.py client 1"],
77+
"commands" : ["sleep 2", "python3 client.py client 1"],
7878
"outgoing_connections" : ["server"]
7979
},
8080
{
@@ -112,7 +112,7 @@
112112
},
113113
{
114114
"container_name" : "client",
115-
"commands" : ["sleep 1", "python3 client.py client 2"],
115+
"commands" : ["sleep 2", "python3 client.py client 2"],
116116
"outgoing_connections" : ["server"]
117117
},
118118
{
@@ -150,7 +150,7 @@
150150
},
151151
{
152152
"container_name" : "client",
153-
"commands" : ["sleep 1", "python3 client.py client 3"],
153+
"commands" : ["sleep 2", "python3 client.py client 3"],
154154
"outgoing_connections" : ["server"]
155155
},
156156
{
@@ -189,7 +189,7 @@
189189
},
190190
{
191191
"container_name" : "client",
192-
"commands" : ["sleep 1", "python3 client.py client 4 udp_enabled"],
192+
"commands" : ["sleep 2", "python3 client.py client 4 udp_enabled"],
193193
"outgoing_connections" : ["server"]
194194
},
195195
{

examples/16_docker_network_python/config/test_input/client.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import traceback
88

99
MY_NAME = ""
10-
KNOWN_HOSTS_TCP = 'knownhosts_tcp.csv'
11-
KNOWN_HOSTS_UDP = 'knownhosts_udp.csv'
10+
KNOWN_HOSTS_TCP = 'knownhosts_tcp.txt'
11+
KNOWN_HOSTS_UDP = 'knownhosts_udp.txt'
1212
USE_UDP = False
1313

1414

@@ -42,29 +42,35 @@ def read_known_hosts_csv():
4242
global server_name
4343
global incoming_tcp_port, outgoing_tcp_port
4444
global incoming_udp_port, outgoing_udp_port
45-
with open(KNOWN_HOSTS_TCP) as csv_file:
46-
csv_reader = csv.reader(csv_file, delimiter=',')
47-
for sender, recv, port in csv_reader:
45+
46+
with open(KNOWN_HOSTS_TCP) as infile:
47+
content = infile.readlines()
48+
49+
for line in content:
50+
sender, recv, port = line.split()
51+
if sender == MY_NAME:
52+
outgoing_tcp_port =port
53+
server_name = recv
54+
elif recv == MY_NAME:
55+
incoming_tcp_port = port
56+
server_name = sender
57+
else:
58+
continue
59+
60+
if USE_UDP:
61+
with open(KNOWN_HOSTS_UDP) as infile:
62+
content = infile.readlines()
63+
64+
for line in content:
65+
sender, recv, port = line.split()
4866
if sender == MY_NAME:
49-
outgoing_tcp_port =port
67+
outgoing_udp_port =port
5068
server_name = recv
5169
elif recv == MY_NAME:
52-
incoming_tcp_port = port
70+
incoming_udp_port = port
5371
server_name = sender
5472
else:
5573
continue
56-
if USE_UDP:
57-
with open(KNOWN_HOSTS_UDP) as csv_file:
58-
csv_reader = csv.reader(csv_file, delimiter=',')
59-
for sender, recv, port in csv_reader:
60-
if sender == MY_NAME:
61-
outgoing_udp_port =port
62-
server_name = recv
63-
elif recv == MY_NAME:
64-
incoming_udp_port = port
65-
server_name = sender
66-
else:
67-
continue
6874

6975
def initialize_incoming_connections():
7076
global incoming_tcp_socket, incoming_udp_socket

examples/16_docker_network_python/config/test_input/router.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,28 @@ def build_switchboard():
5757
try:
5858
#Read the known_hosts.csv see the top of the file for the specification
5959
for connection_type in ["tcp", "udp"]:
60-
filename = 'knownhosts_{0}.csv'.format(connection_type)
60+
filename = 'knownhosts_{0}.txt'.format(connection_type)
6161
with open(filename, 'r') as infile:
62-
reader = csv.reader(infile)
63-
for sender, recipient, port in reader:
64-
#Strip away trailing or leading whitespace
65-
sender = '{0}_Actual'.format(sender.strip())
66-
recipient = '{0}_Actual'.format(recipient.strip())
67-
port = port.strip()
68-
69-
if not port in PORTS:
70-
PORTS.append(port)
71-
else:
72-
raise SystemExit("ERROR: port {0} was encountered twice. Please keep all ports independant.".format(port))
73-
74-
SWITCHBOARD[port] = {}
75-
SWITCHBOARD[port]['connection_type'] = connection_type
76-
SWITCHBOARD[port]['sender'] = sender
77-
SWITCHBOARD[port]['recipient'] = recipient
78-
SWITCHBOARD[port]['connected'] = False
79-
SWITCHBOARD[port]['connection'] = None
62+
content = infile.readlines()
63+
64+
for line in content:
65+
sender, recipient, port = line.split()
66+
#Strip away trailing or leading whitespace
67+
sender = '{0}_Actual'.format(sender.strip())
68+
recipient = '{0}_Actual'.format(recipient.strip())
69+
port = port.strip()
70+
71+
if not port in PORTS:
72+
PORTS.append(port)
73+
else:
74+
raise SystemExit("ERROR: port {0} was encountered twice. Please keep all ports independant.".format(port))
75+
76+
SWITCHBOARD[port] = {}
77+
SWITCHBOARD[port]['connection_type'] = connection_type
78+
SWITCHBOARD[port]['sender'] = sender
79+
SWITCHBOARD[port]['recipient'] = recipient
80+
SWITCHBOARD[port]['connected'] = False
81+
SWITCHBOARD[port]['connection'] = None
8082

8183

8284
except IOError as e:

examples/16_docker_network_python/submissions/correct/server.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#!/usr/bin/env python3
22
import socket
33
import os
4-
import csv
54
import sys
65
import time
76
import traceback
87

98
MY_NAME = ""
10-
KNOWN_HOSTS_CSV = 'knownhosts_tcp.csv'
9+
KNOWN_HOSTS_TCP = 'knownhosts_tcp.txt'
1110

1211
# This tutorial is kept simple intentionally rather than using data structures or
1312
# dictionaries to store these values.
@@ -19,28 +18,30 @@
1918
open_connection = None
2019

2120
def init():
22-
read_known_hosts_csv()
21+
read_known_hosts_tcp()
2322
initialize_incoming_connections()
2423

2524

2625
#knownhosts_tcp.csv and knownhosts_udp.csv are of the form
2726
#sender,recipient,port_number
2827
# such that sender sends all communications to recipient via port_number.
29-
def read_known_hosts_csv():
28+
def read_known_hosts_tcp():
3029
global client_name
3130
global incoming_port
3231
global outgoing_port
33-
with open(KNOWN_HOSTS_CSV) as csv_file:
34-
csv_reader = csv.reader(csv_file, delimiter=',')
35-
for sender, recv, port in csv_reader:
36-
if sender == MY_NAME:
37-
outgoing_port =port
38-
client_name = recv
39-
elif recv == MY_NAME:
40-
incoming_port = port
41-
client_name = sender
42-
else:
43-
continue
32+
with open(KNOWN_HOSTS_TCP) as infile:
33+
content = infile.readlines()
34+
35+
for line in content:
36+
sender, recv, port = line.split()
37+
if sender == MY_NAME:
38+
outgoing_port =port
39+
client_name = recv
40+
elif recv == MY_NAME:
41+
incoming_port = port
42+
client_name = sender
43+
else:
44+
continue
4445

4546
def initialize_incoming_connections():
4647
global incoming_socket

examples/16_docker_network_python/submissions/incorrect/server.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#!/usr/bin/env python3
22
import socket
33
import os
4-
import csv
54
import sys
65
import time
76
import traceback
87

98
MY_NAME = ""
10-
KNOWN_HOSTS_CSV = 'knownhosts_tcp.csv'
9+
KNOWN_HOSTS_TCP = 'knownhosts_tcp.txt'
1110

1211
# This tutorial is kept simple intentionally rather than using data structures or
1312
# dictionaries to store these values.
@@ -19,28 +18,30 @@
1918
open_connection = None
2019

2120
def init():
22-
read_known_hosts_csv()
21+
read_known_hosts_tcp()
2322
initialize_incoming_connections()
2423

2524

2625
#knownhosts_tcp.csv and knownhosts_udp.csv are of the form
2726
#sender,recipient,port_number
2827
# such that sender sends all communications to recipient via port_number.
29-
def read_known_hosts_csv():
28+
def read_known_hosts_tcp():
3029
global client_name
3130
global incoming_port
3231
global outgoing_port
33-
with open(KNOWN_HOSTS_CSV) as csv_file:
34-
csv_reader = csv.reader(csv_file, delimiter=',')
35-
for sender, recv, port in csv_reader:
36-
if sender == MY_NAME:
37-
outgoing_port =port
38-
client_name = recv
39-
elif recv == MY_NAME:
40-
incoming_port = port
41-
client_name = sender
42-
else:
43-
continue
32+
with open(KNOWN_HOSTS_TCP) as infile:
33+
content = infile.readlines()
34+
35+
for line in content:
36+
sender, recv, port = line.split()
37+
if sender == MY_NAME:
38+
outgoing_port =port
39+
client_name = recv
40+
elif recv == MY_NAME:
41+
incoming_port = port
42+
client_name = sender
43+
else:
44+
continue
4445

4546
def initialize_incoming_connections():
4647
global incoming_socket

examples/16_docker_network_python/submissions/infinite_response/server.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#!/usr/bin/env python3
22
import socket
33
import os
4-
import csv
54
import sys
65
import time
76
import traceback
87

98
MY_NAME = ""
10-
KNOWN_HOSTS_CSV = 'knownhosts_tcp.csv'
9+
KNOWN_HOSTS_TCP = 'knownhosts_tcp.txt'
1110

1211
# This tutorial is kept simple intentionally rather than using data structures or
1312
# dictionaries to store these values.
@@ -19,27 +18,30 @@
1918
open_connection = None
2019

2120
def init():
22-
read_known_hosts_csv()
21+
read_known_hosts_tcp()
2322
initialize_incoming_connections()
2423

24+
2525
#knownhosts_tcp.csv and knownhosts_udp.csv are of the form
2626
#sender,recipient,port_number
2727
# such that sender sends all communications to recipient via port_number.
28-
def read_known_hosts_csv():
28+
def read_known_hosts_tcp():
2929
global client_name
3030
global incoming_port
3131
global outgoing_port
32-
with open(KNOWN_HOSTS_CSV) as csv_file:
33-
csv_reader = csv.reader(csv_file, delimiter=',')
34-
for sender, recv, port in csv_reader:
35-
if sender == MY_NAME:
36-
outgoing_port =port
37-
client_name = recv
38-
elif recv == MY_NAME:
39-
incoming_port = port
40-
client_name = sender
41-
else:
42-
continue
32+
with open(KNOWN_HOSTS_TCP) as infile:
33+
content = infile.readlines()
34+
35+
for line in content:
36+
sender, recv, port = line.split()
37+
if sender == MY_NAME:
38+
outgoing_port =port
39+
client_name = recv
40+
elif recv == MY_NAME:
41+
incoming_port = port
42+
client_name = sender
43+
else:
44+
continue
4345

4446
def initialize_incoming_connections():
4547
global incoming_socket

examples/16_docker_network_python/submissions/large_response/server.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#!/usr/bin/env python3
22
import socket
33
import os
4-
import csv
54
import sys
65
import time
76
import traceback
87

98
MY_NAME = ""
10-
KNOWN_HOSTS_CSV = 'knownhosts_tcp.csv'
9+
KNOWN_HOSTS_TCP = 'knownhosts_tcp.txt'
1110

1211
# This tutorial is kept simple intentionally rather than using data structures or
1312
# dictionaries to store these values.
@@ -19,28 +18,30 @@
1918
open_connection = None
2019

2120
def init():
22-
read_known_hosts_csv()
21+
read_known_hosts_tcp()
2322
initialize_incoming_connections()
2423

2524

2625
#knownhosts_tcp.csv and knownhosts_udp.csv are of the form
2726
#sender,recipient,port_number
2827
# such that sender sends all communications to recipient via port_number.
29-
def read_known_hosts_csv():
28+
def read_known_hosts_tcp():
3029
global client_name
3130
global incoming_port
3231
global outgoing_port
33-
with open(KNOWN_HOSTS_CSV) as csv_file:
34-
csv_reader = csv.reader(csv_file, delimiter=',')
35-
for sender, recv, port in csv_reader:
36-
if sender == MY_NAME:
37-
outgoing_port =port
38-
client_name = recv
39-
elif recv == MY_NAME:
40-
incoming_port = port
41-
client_name = sender
42-
else:
43-
continue
32+
with open(KNOWN_HOSTS_TCP) as infile:
33+
content = infile.readlines()
34+
35+
for line in content:
36+
sender, recv, port = line.split()
37+
if sender == MY_NAME:
38+
outgoing_port =port
39+
client_name = recv
40+
elif recv == MY_NAME:
41+
incoming_port = port
42+
client_name = sender
43+
else:
44+
continue
4445

4546
def initialize_incoming_connections():
4647
global incoming_socket

0 commit comments

Comments
 (0)