This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
52 lines (43 loc) · 1.82 KB
/
controller.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
#!/usr/bin/python
import sys
import socket, optparse
import json
from message import Message
def get_files(message):
"""returns string file name retrieved from message"""
message_deserialized = json.loads(message)
filename = message_deserialized['contents'] #returns unicode
return str(filename)
def main():
parser = optparse.OptionParser()
parser.add_option('--is', dest='ips', default='10.0.0.1')
parser.add_option('--ir', dest='ipr', default='10.0.0.3')
parser.add_option('-c', dest='command', type='int', default='0')
parser.add_option('-f', dest='filename', default='sample.txt')
(options, args) = parser.parse_args()
port_CtoS = 50000
port_RtoC = 50001
command = options.command
filename = options.filename
if not(command == 1 or command == 2 or command == 3 or command == 4 or command == 5):
print('ERROR REQUEST TYPE. PLEASE TYPE NUMBER FROM 1 to 5. \n e.g.) python controller.py -c 3\n')
sys.exit()
else:
if(command == 1):
CtoS_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
CtoS_socket.connect((options.ips, port_CtoS))
message = Message(command=command)
CtoS_socket.send(message.export())
response = CtoS_socket.recv(4096)
message_rec = Message()
message_rec.decode(response)
print("Available files are: \n{}".format(message_rec.payload))
else:
RtoC_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
RtoC_socket.connect((options.ipr, port_RtoC))
message = Message(command=command, filename=filename)
RtoC_socket.send(message.export())
response = RtoC_socket.recv(4096)
print("Received from rendedrer: {}".format(response))
if __name__ == '__main__':
main()