-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClient.py
More file actions
executable file
·132 lines (98 loc) · 4.24 KB
/
Copy pathClient.py
File metadata and controls
executable file
·132 lines (98 loc) · 4.24 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
#!/usr/bin/python
# USAGE: python Client.py [serverPort]
import sys, socket, os
hostName = (sys.argv[1])
controlPort = int(sys.argv[2])
hostAddress = socket.gethostbyname(hostName)
# Directory where client files are stored
ClientFolder = os.getcwd() + "/ClientFolder"
controlConnection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
controlConnection.connect((hostAddress, controlPort))
def GetInput():
# Prompt to accept and send commands to server
command = raw_input("ftp> ")
# Before sending to server, validate if it is a put
# that the file exists.
if command[0:3] == "put":
if not os.path.isfile(ClientFolder + "/" + command[4:] ):
print "This file does not exist."
command = GetInput()
return command
# Loop to send commands to the server
while True:
# Get user command
command = GetInput()
# Send command to server
controlConnection.send(command)
# Receive response from server
serverResponse = controlConnection.recv(1024)
if not serverResponse: break
#print '[Control] Server says "%s"' % serverResponse
if command == "ls":
print serverResponse
# Interpret server response and act accordingly.
# Case where request to get a file and it exists
# if file does not exists nothing happens. Error is displayed.
if command[0:3] == "get" and serverResponse == "File exists." :
targetFile = command[4:]
# Initilize data connection
dataConnection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dataConnection.bind(('', 0))
dataConnection.listen(1)
dataPort = dataConnection.getsockname()[1]
# Send data connection port to server over control connection
# so server can connect.
controlConnection.send(str(dataPort))
# Wait for server to connect.
dataConnection, maddr = dataConnection.accept()
#print '[Control] Got connection from', maddr
# Used in case the file name needs to be changed.
# Ex. file already exists.
newFile = targetFile
counter = 1
# Determine if the filename already exists
if os.path.isfile(ClientFolder + "/" + targetFile):
while os.path.isfile(ClientFolder + "/"+ str(counter) + targetFile):
counter = counter + 1
newFile = str(counter) + targetFile
# Recieve file from server
f = open(ClientFolder + "/" + newFile,"wb")
while 1:
data = dataConnection.recv(1024)
if not data: break
f.write(data)
f.close()
# Close data connection
dataConnection.close()
# Print details
byteSize = os.path.getsize(ClientFolder + "/" + newFile)
print "Downloaded - " + targetFile + " (" + str(byteSize) + " bytes)"
# Case where request to upload a file
if command[0:3] == "put":
targetFile = command[4:]
# Initilize data connection
dataConnection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dataConnection.bind(('', 0))
dataConnection.listen(1)
dataPort = dataConnection.getsockname()[1]
# Send data connection port to server over control connection
# so server can connect.
controlConnection.send(str(dataPort))
# Wait for server to connect.
dataConnection, maddr = dataConnection.accept()
#print '[Control] Got connection from', maddr
# Send file to server
f = open(ClientFolder + '/' + targetFile, "rb")
targetFileData = f.read()
f.close()
dataConnection.send(targetFileData)
# Close data connection
dataConnection.close()
# Print details
byteSize = os.path.getsize(ClientFolder + "/" + targetFile)
print "Uploaded - " + targetFile + " (" + str(byteSize) + " bytes)"
# If command is quit, exit loop and close program.
if command == "quit":
break
# Close control connection
controlConnection.close()