-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftclient
More file actions
executable file
·168 lines (150 loc) · 4.76 KB
/
Copy pathftclient
File metadata and controls
executable file
·168 lines (150 loc) · 4.76 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
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# ftclient
# CS372 Spring 2018
# -----------------
# Name: Zachary Thomas
# Email: thomasza@oregonstate.edu
# Date: 5/19/2018
# ------------------------------------
# Set up file transfer with server.
# ------------------------------------
# Cited references:
# Lecture 15: Socket Programming Primer
from socket import *
import sys
import time
import os.path
# validateInput
# -------------
# Confirm that the command line arguments are valid.
def validateInput():
#confirm command line arguments include port number
if (len(sys.argv) < 5 or len(sys.argv) > 6):
print "Useage: " + str(sys.argv[0]) + " [server address] [server port] [command] [filename] [data port]"
sys.exit(0)
#confirm a valid command was used
if (sys.argv[3] != "-l" and sys.argv[3] != "-g"):
print "Invalid command. Valid commands are -g or -l"
sys.exit(0)
# initiateContact
# -------
# Sets up a control connection with a ftserver.
#
# controlSocket: The control socket.
# serverName: The address of the server.
# serverPort: The control port of the server.
def initiateContact(controlSocket, serverName, serverPort):
#attempt to connect to the server
try:
controlSocket.connect((serverName, serverPort))
except:
print "Failed to connect to server"
sys.exit(1)
# makeRequest
# ------------
# Send a request to the server.
# The request will either be for listing
# the contents of the directory (-l)
# or for a file (-g).
#
# dataPort: The data port.
# command: The command requested.
# fileName: The name of the file requested.
# controlSocket: The control socket.
# serverName: The address of the server.
def makeRequest(dataPort, command, fileName, controlSocket, serverName):
#send a transfer request
request = str(dataPort) + command + serverName + "@" + fileName + "\n"
controlSocket.send(request)
# receiveFile
# ------------
# Connect to data port of ftserver.
# Once connected receive file from server on data port.
#
# dataSocket: The data socket.
# controlSocket: The control socket.
# serverName: The address of the server.
# serverPort: The control port of the server.
# dataPort: The data port of the server.
# fileName: The name of the file requested.
def receiveFile(dataSocket, controlSocket, serverName, serverPort, dataPort, fileName):
#attempt to connect to data port for transfer
time.sleep(2)
try:
dataSocket.connect((serverName, dataPort))
except:
print "Failed to connect to server data port"
controlSocket.close()
sys.exit(1)
#receive and print message from server
fileContents = ""
while "!$@$!" not in fileContents:
fileContents += controlSocket.recv(1000)
fileContents = fileContents[:-5]
print fileContents
#if message says the file was not found we are done
if "FILE NOT FOUND" in fileContents:
dataSocket.close()
controlSocket.close()
sys.exit(0)
#receive file
if fileName:
#keep receiving until we get the whole file
fileContents = ""
while "!$@$!" not in fileContents:
fileContents += dataSocket.recv(5000)
fileContents = fileContents[:-5]
#close connections
dataSocket.close()
controlSocket.close()
#confirm that file does not already exist in directory
if (os.path.isfile(fileName)):
print fileName + " already exists. Do you want to overwrite the file?"
owFile = raw_input("[y/n]: ")
while owFile != "y" and owFile != "yes":
if owFile == "n" or owFile == "no":
print "No file transfered."
#close both connections
dataSocket.close()
controlSocket.close()
sys.exit(1)
else:
print "Bad input. Please enter y or n."
owFile = raw_input("[y/n]: ")
with open(fileName, "w") as text_file:
text_file.write(fileContents)
print "File transfer complete."
sys.exit(0)
#receive list from server
fileContents = ""
while "!$@$!" not in fileContents:
fileContents = dataSocket.recv(1000)
fileContents = fileContents[:-5]
print fileContents
#close both connections
dataSocket.close()
controlSocket.close()
def main():
#confirm that input was valid
validateInput()
#define command line arguments by name
serverName = sys.argv[1]
serverPort = int(sys.argv[2])
command = sys.argv[3]
#if we don't enter a file name then accept argument four as the data port
if (len(sys.argv) == 6):
fileName = sys.argv[4]
dataPort = int(sys.argv[5])
else:
dataPort = int(sys.argv[4])
fileName = ''
#connect to a ftserver on the control port
controlSocket = socket(AF_INET, SOCK_STREAM)
initiateContact(controlSocket, serverName, serverPort)
#send a request for a file or list to server
makeRequest(dataPort, command, fileName, controlSocket, serverName)
#establish data port connection and receive file
dataSocket = socket(AF_INET, SOCK_STREAM)
receiveFile(dataSocket, controlSocket, serverName, serverPort, dataPort, fileName)
if __name__ == "__main__":
main()