-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.py
82 lines (51 loc) · 1.29 KB
/
ls.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
# -*- coding: utf-8 -*-
# Víctor A. Hernández & José R. Ortiz
# CCOM 4017 – Operating Systems
# Assignment 4: mds_db.py (comes with 7 other files)
# List client for the DFS
import sys
import socket
from Packet import *
LS_BUFFER = 4096 # must be big if inserting many files into DFS
def usage():
print """Usage: python %s <server>:<port, default=8000>""" % sys.argv[0]
sys.exit(0)
def client(ip, port):
# Contacts the metadata server
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
# Ask for list of files
p = Packet()
p.BuildListPacket()
sock.send(p.getEncodedPacket())
# Receive msg from MDS
msg = sock.recv(LS_BUFFER)
if msg == "NAK":
print "Error ocurred when receiving `ls` response! Exiting..."
sock.close()
sys.exit(0)
# Decode msg
p.DecodePacket(msg)
# Get list of (fname, fsize)
flist = p.getFileArray()
# Print fname followed by fsize
for n, s in flist:
print "%s %d bytes" % (n,s)
# Close connection
sock.close()
def main():
if len(sys.argv) < 2:
usage()
ip, port = None, None
server = sys.argv[1].split(":")
if len(server) == 1:
ip = server[0]
port = 8000
elif len(server) == 2:
ip = server[0]
port = int(server[1])
if ip is None:
usage()
client(ip, port)
if __name__ == "__main__":
main()