-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
81 lines (74 loc) · 2.03 KB
/
client.py
File metadata and controls
81 lines (74 loc) · 2.03 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
import socket
import sys
import os
import time
def usage():
###Function that shows to the user how to call the program
print("Usage:")
print("python "+__file__+" <host> <port> <file path>")
sys.exit()
def sendFile(host, port, fp):
###Function that manages the sending of the file to the server
m = s.recv(1024)
if (m=="READY"):
print("Sending file to "+host+":"+str(port))
try:
f = open(fp, 'rb')
d = f.read(4096)
s.send(d)
while d != "":
d = f.read(4096)
s.send(d)
#Get a time for the socket not concatenate the last send with the next
#and signal the server that the file has reached the end
time.sleep(0.1)
s.send("--END--")
f.close()
l = s.recv(1024)
#If the file has been uploaded successfully
#Get the total value from the products of the uploaded file
if (l=="SUCCES"):
print("Succesfully uploaded file.")
totalValue = s.recv(1024)
print("Total products value: "+totalValue)
sys.exit()
else:
print("Failed to upload file. Try again?")
s.close()
except Exception as msg:
print("Error message: "+str(msg))
return False
return True
elif (m=="ERROR"):
print("Error: An unexpected error has occoured at the server side. Try again?")
return False
else:
print("Error: Didn't expect this message: "+m)
return False
###Try to get all paramms from the command line
###If not, show to te user the usage information
try:
host = sys.argv[1]
port = int(sys.argv[2])
filePath = sys.argv[3]
except:
usage()
################################################
#Create a socket that use IPV4 and TCP protocol
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#If the file exists
#Start the connection with the server
if (os.path.exists(filePath)):
try:
s.connect((host, int(port)))
print("Connected to server!")
except socket.error as sem:
print("ERROR: Couldn't connect.")
print(sem)
sys.exit()
##Send a message that signals the start of the file upload
s.send("GETFILE")
sendFile(host, port, filePath)
else:
print("File does not exists.")
sys.exit()