forked from ImWayGooderest/Networking-Assignment-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
136 lines (89 loc) · 3.28 KB
/
cli.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
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
__author__ = 'BrendonH'
import sys
import socket
#from ephemeral.py, not entirely sure how to use it yet
def getEphemeralPort():
# Create a socket
welcomeSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to port 0
welcomeSocket.bind(('',0))
# Retreive the ephemeral port number
print("I chose ephemeral port: ", welcomeSocket.getsockname()[1])
return welcomeSocket
def put(dataSock, fileName):
#need to check if file name is valid
# The name of the file
# fileName = "file.txt"
# Open the file
fileObj = open(fileName, "r")
# The number of bytes sent
numSent = 0
# The file data
fileData = None
# Keep sending until all is sent
while True:
# Read 65536 bytes of data
fileData = fileObj.read(65536)
# Make sure we did not hit EOF
if fileData:
# Get the size of the data read
# and convert it to string
dataSizeStr = str(len(fileData))
# Prepend 0's to the size string
# until the size is 10 bytes
while len(dataSizeStr) < 10:
dataSizeStr = "0" + dataSizeStr
# Prepend the size of the data to the
# file data.
fileData = dataSizeStr + fileData
# The number of bytes sent
numSent = 0
# Send the data!
while len(fileData) > numSent:
numSent += dataSock.send(fileData[numSent:].encode('ascii')) #this is where it breaks right now
# The file has been read. We are done
else:
break
print("Sent ", numSent, " bytes.")
# Close the socket and the file
dataSock.close()
fileObj.close()
def sendCommand(commandString, connSock):
# send command
commandString = str(len(commandString)) + " " + commandString #prepend size of command string
bytes_sent = 0
while bytes_sent < len(commandString):
bytes_sent += connSock.send(commandString.encode('ascii'))
def main(argv):
# Command line checks
if len(argv) < 2:
print("USAGE python " + argv[0] + " <FILE NAME>")
while True:
# Server address
serverAddr = argv[1]
# Server port
serverPort = int(argv[2])
# Create a TCP socket
connSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the server
connSock.connect((serverAddr, serverPort))
commandString = input("ftp> ")
command = commandString.split() #splits string into list of words
if(command[0].lower() == "get"):
pass #pass is a placeholder for the eventual function get() function that will be called
elif(command[0].lower() == "put"):
dataSock = getEphemeralPort()
commandString = commandString + " " + str(dataSock.getsockname()[1])
dataSock.listen(1)
sendCommand(commandString, connSock)
dataSock.accept()
put(dataSock, command[1])
elif(command[0].lower() == "ls"):
pass
elif(command[0].lower() == "quit"):
#function for sending quit goes here
sys.exit(0)
else:
print("Invalid command")
if __name__ == "__main__":
main(sys.argv)