-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
159 lines (149 loc) · 5.73 KB
/
Copy pathclient.py
File metadata and controls
159 lines (149 loc) · 5.73 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
150
151
152
153
154
155
156
157
158
159
'''111
Script for client side
@author: hao
'''
import protocol
import config
from socket import *
import os
class client:
fileList=[] # list to store the file information
#Constructor: load client configuration from config file
def __init__(self):
self.serverName, self.serverPort, self.clientPort, self.downloadPath = config.config().readClientConfig()
# Function to produce user menu
def printMenu(self):
print("Welcome to simple file sharing system!")
print("Please select operations from menu")
print("--------------------------------------")
print("1. Review the List of Available Files")
print("2. Download File")
print("3. Upload a file to the Server")
print("4. Quit")
# Function to get user selection from the menu
def getUserSelection(self):
ans=0
# only accept option 1-4
while ans>4 or ans<1:
self.printMenu()
try:
ans=int(input())
except:
ans=0
if (ans<=4) and (ans>=1):
return ans
print("Invalid Option")
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Build connection to server
def connect(self):
serverName = self.serverName
serverPort = self.serverPort
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
return clientSocket
# Get file list from server by sending the request
def getFileList(self):
mySocket=self.connect()
mySocket.send(protocol.prepareMsg(protocol.HEAD_REQUEST," "))
header, msg=protocol.decodeMsg(mySocket.recv(1024).decode())
mySocket.close()
if(header==protocol.HEAD_LIST):
files=msg.split(",")
self.fileList=[]
for f in files:
self.fileList.append(f)
else:
print ("Error: cannnot get file list!")
#==========================================================================
def getFileListFromClient(self):
files= os.listdir(self.downloadPath)
self.fileList=[]
for f in files:
self.fileList.append(f)
#==========================================================================
# function to print files in the list with the file number
def printFileList(self):
count=0
for f in self.fileList:
count+=1
print('{:<3d}{}'.format(count,f))
# Function to select the file from file list by file number,
# return the file name user selected
def selectDownloadFile(self):
if(len(self.fileList)==0):
self.getFileList()
ans=-1
while ans<0 or ans>len(self.fileList)+1:
self.printFileList()
print("Please select the file you want to download from the list (enter the number of files):")
try:
ans=int(input())
except:
ans=-1
if (ans>0) and (ans<len(self.fileList)+1):
return self.fileList[ans-1]
print("Invalid number")
#--------------------------------------------------------------------------------------------------------------
def selectUploadFile(self):
if(len(self.fileList)==0):
self.getFileListFromClient()
ans=-1
while ans<0 or ans>len(self.fileList)+1:
self.printFileList()
print("Please select the file you want to upload from the list (enter the number of files):")
try:
ans=int(input())
except:
ans=-1
if (ans>0) and (ans<len(self.fileList)+1):
return self.fileList[ans-1]
print("Invalid number")
#------------------------------------------------------------------------------------------------------------------
# Function to send download request to server and wait for file data
def downloadFile(self,fileName):
mySocket=self.connect()
mySocket.send(protocol.prepareMsg(protocol.HEAD_DOWNLOAD, fileName))
with open(self.downloadPath+"/"+fileName, 'wb') as f:
print ('file opened')
while True:
#print('receiving data...')
data = mySocket.recv(1024)
#print('data=%s', (data))
if not data:
break
# write data to a file
f.write(data)
print(fileName+" has been downloaded!")
mySocket.close()
#==========================================================================================
def uploadFile(self,fileName):
mySocket=self.connect()
mySocket.send(protocol.prepareMsg(protocol.HEAD_UPLOAD, fileName))
f = open(self.downloadPath+"/"+fileName,'rb')
l = f.read(1024) # each time we only send 1024 bytes of data
while (l):
mySocket.send(l)
l = f.read(1024)
print(fileName+" has been Uploaded!")
mySocket.close()
#============================================================================================
# Main logic of the client, start the client application
def start(self):
opt=0
while opt!=4:
opt=self.getUserSelection()
if opt==1:
if(len(self.fileList)==0):
self.getFileList()
self.printFileList()
elif opt==2:
self.downloadFile(self.selectDownloadFile())
elif opt==3:
self.getFileListFromClient()
self.uploadFile(self.selectUploadFile())
else:
pass
def main():
c=client()
c.start()
main()