# *******************************************************************
# Trang Nguyen trang_nguyen@csu.fullerton.edu CWID:802816165
# Insert your name here
# 
# CPSC 471 - Section 2
# *******************************************************************

import sys
import os
import subprocess
import socket
import pickle #for receiving list data

bufferSize = 4096
serverName = "localhost"

# *******************************************************************
#							FUNCTIONS
# We need to write these functions:
# a) Receives the specified number of bytes from a specified socket
# b) Gets size of a file in bytes
# c) Upload a file to server
# d) Download a file from server
# e) List file from the server
# f) quit (disconnect/exit)
# *******************************************************************


def recvAll(sock, numBytes):

	# The buffer
	recvBuff = ""
	
	# Keep receiving till all is received
	while len(recvBuff) < numBytes:
		# temporary buffer
		# Attempt to receive bytes
		tmpBuff =  sock.recv(numBytes)
		
		# The other side has closed the socket
		if not tmpBuff:
			break
		
		# Add the received bytes to the buffer
		recvBuff += tmpBuff
	
	return recvBuff

# Function to create a socket using a provided port number
def createSocket(portNum):

	# Create a TCP socket
	connSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	
	# Connect to server
	connSock.connect((serverName, int (portNum)))
	print ("Connected to port #", portNum)
	
	# Return a socket
	return connSock

	
def sizeOfFile (fileName):
	statinfo = os.stat(fileName)
	size = statinfo.st_size
	return size

def uploadFileToServer(fileName, portNumb):

	#generate an emphemeral port
	tempSocket=createSocket(portNumb)

	#open file 
	try:
		file_object=open(fileName,'r')
	except OSError:
		print ("can not open file %s to read", fileName)
		tempSocket.close()

	fileSize=sizeOfFile(fileName)
	print ("Uploading", fileName,"to server")

	while True:
		# Read  data
		fileData = file_object.read(fileSize)
	
		# 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 += tempSocket.send(fileData[numSent:])
	
		# The file has been read. We are done
		else:
			break

		print ("Sent ", numSent, " bytes.")
	
	# Close the socket and the file
	file_object.close()
	tempSocket.close()
	
	

#def downloadFileFromServer(socket,fileName):





# *******************************************************************
#							MAIN PROGRAM
# *******************************************************************


#if client command line has 3 args. for ex: python client.py localhost 1234

if len(sys.argv) < 3:
	print ("python " + sys.argv[0]+"<server_machine>"+"<server_port>")

serverName=sys.argv[1]
serverPort=int (sys.argv[2])

primarySocket= createSocket(serverPort)

ans = ""
fileName = ""
success = ""

while (ans != "quit"):
	ans =raw_input("ftp> ")

	#argument counting with spaces
	ftp_arg_count = ans.count(' ')
	if ftp_arg_count == 1:
		(command, rest) = ans.split()
	elif ftp_arg_count == 0:
		command = ans

	if command == "put" and ftp_arg_count == 1:
		fileName = rest
		primarySocket.send(ans)
		tempPort=primarySocket.recv(bufferSize)
		print ("Ephermeral port # ",tempPort)
		success = uploadFileToServer(fileName,tempPort)
		if success == 0:
			print ("fail to upload")
		else:
			print("Finish uploading")
	elif command == "get" and ftp_arg_count == 1:
		fileName = rest
	elif command == "ls" and ftp_arg_count == 0:
		#NOTE: not sure how buffer works so just used bufferSize for the recieves -andre

		primarySocket.send(ans)
		print("sending ls request to server")

		#get ephemeral port generated by server
		ephPort = primarySocket.recv(bufferSize)
		print ("Ephermeral port # ",ephPort)

		#create ephemeral socket and wait for data
		ephSocket = createSocket(ephPort)
		data = ephSocket.recv(bufferSize)

		#need 'pickle.loads' to extract list
		server_dir = pickle.loads(data)

		#print out directory
		for line in server_dir:
			print(line)

		ephSocket.close()

	elif command == "quit" and ftp_arg_count == 0:
		primarySocket.send(ans)
		print ("closing now")
		primarySocket.close()
	else:
		print ("not a valid command")

		








2980c9802572f08b1370ba22808512bd80e33ce6
<mask token>


def recvAll(sock, numBytes):
    recvBuff = ''
    while len(recvBuff) < numBytes:
        tmpBuff = sock.recv(numBytes)
        if not tmpBuff:
            break
        recvBuff += tmpBuff
    return recvBuff


def createSocket(portNum):
    connSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    connSock.connect((serverName, int(portNum)))
    print('Connected to port #', portNum)
    return connSock


def sizeOfFile(fileName):
    statinfo = os.stat(fileName)
    size = statinfo.st_size
    return size


def uploadFileToServer(fileName, portNumb):
    tempSocket = createSocket(portNumb)
    try:
        file_object = open(fileName, 'r')
    except OSError:
        print('can not open file %s to read', fileName)
        tempSocket.close()
    fileSize = sizeOfFile(fileName)
    print('Uploading', fileName, 'to server')
    while True:
        fileData = file_object.read(fileSize)
        if fileData:
            dataSizeStr = str(len(fileData))
            while len(dataSizeStr) < 10:
                dataSizeStr = '0' + dataSizeStr
            fileData = dataSizeStr + fileData
            numSent = 0
            while len(fileData) > numSent:
                numSent += tempSocket.send(fileData[numSent:])
        else:
            break
        print('Sent ', numSent, ' bytes.')
    file_object.close()
    tempSocket.close()


<mask token>

<mask token>


def recvAll(sock, numBytes):
    recvBuff = ''
    while len(recvBuff) < numBytes:
        tmpBuff = sock.recv(numBytes)
        if not tmpBuff:
            break
        recvBuff += tmpBuff
    return recvBuff


def createSocket(portNum):
    connSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    connSock.connect((serverName, int(portNum)))
    print('Connected to port #', portNum)
    return connSock


def sizeOfFile(fileName):
    statinfo = os.stat(fileName)
    size = statinfo.st_size
    return size


def uploadFileToServer(fileName, portNumb):
    tempSocket = createSocket(portNumb)
    try:
        file_object = open(fileName, 'r')
    except OSError:
        print('can not open file %s to read', fileName)
        tempSocket.close()
    fileSize = sizeOfFile(fileName)
    print('Uploading', fileName, 'to server')
    while True:
        fileData = file_object.read(fileSize)
        if fileData:
            dataSizeStr = str(len(fileData))
            while len(dataSizeStr) < 10:
                dataSizeStr = '0' + dataSizeStr
            fileData = dataSizeStr + fileData
            numSent = 0
            while len(fileData) > numSent:
                numSent += tempSocket.send(fileData[numSent:])
        else:
            break
        print('Sent ', numSent, ' bytes.')
    file_object.close()
    tempSocket.close()


if len(sys.argv) < 3:
    print('python ' + sys.argv[0] + '<server_machine>' + '<server_port>')
<mask token>
while ans != 'quit':
    ans = raw_input('ftp> ')
    ftp_arg_count = ans.count(' ')
    if ftp_arg_count == 1:
        command, rest = ans.split()
    elif ftp_arg_count == 0:
        command = ans
    if command == 'put' and ftp_arg_count == 1:
        fileName = rest
        primarySocket.send(ans)
        tempPort = primarySocket.recv(bufferSize)
        print('Ephermeral port # ', tempPort)
        success = uploadFileToServer(fileName, tempPort)
        if success == 0:
            print('fail to upload')
        else:
            print('Finish uploading')
    elif command == 'get' and ftp_arg_count == 1:
        fileName = rest
    elif command == 'ls' and ftp_arg_count == 0:
        primarySocket.send(ans)
        print('sending ls request to server')
        ephPort = primarySocket.recv(bufferSize)
        print('Ephermeral port # ', ephPort)
        ephSocket = createSocket(ephPort)
        data = ephSocket.recv(bufferSize)
        server_dir = pickle.loads(data)
        for line in server_dir:
            print(line)
        ephSocket.close()
    elif command == 'quit' and ftp_arg_count == 0:
        primarySocket.send(ans)
        print('closing now')
        primarySocket.close()
    else:
        print('not a valid command')

<mask token>
bufferSize = 4096
serverName = 'localhost'


def recvAll(sock, numBytes):
    recvBuff = ''
    while len(recvBuff) < numBytes:
        tmpBuff = sock.recv(numBytes)
        if not tmpBuff:
            break
        recvBuff += tmpBuff
    return recvBuff


def createSocket(portNum):
    connSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    connSock.connect((serverName, int(portNum)))
    print('Connected to port #', portNum)
    return connSock


def sizeOfFile(fileName):
    statinfo = os.stat(fileName)
    size = statinfo.st_size
    return size


def uploadFileToServer(fileName, portNumb):
    tempSocket = createSocket(portNumb)
    try:
        file_object = open(fileName, 'r')
    except OSError:
        print('can not open file %s to read', fileName)
        tempSocket.close()
    fileSize = sizeOfFile(fileName)
    print('Uploading', fileName, 'to server')
    while True:
        fileData = file_object.read(fileSize)
        if fileData:
            dataSizeStr = str(len(fileData))
            while len(dataSizeStr) < 10:
                dataSizeStr = '0' + dataSizeStr
            fileData = dataSizeStr + fileData
            numSent = 0
            while len(fileData) > numSent:
                numSent += tempSocket.send(fileData[numSent:])
        else:
            break
        print('Sent ', numSent, ' bytes.')
    file_object.close()
    tempSocket.close()


if len(sys.argv) < 3:
    print('python ' + sys.argv[0] + '<server_machine>' + '<server_port>')
serverName = sys.argv[1]
serverPort = int(sys.argv[2])
primarySocket = createSocket(serverPort)
ans = ''
fileName = ''
success = ''
while ans != 'quit':
    ans = raw_input('ftp> ')
    ftp_arg_count = ans.count(' ')
    if ftp_arg_count == 1:
        command, rest = ans.split()
    elif ftp_arg_count == 0:
        command = ans
    if command == 'put' and ftp_arg_count == 1:
        fileName = rest
        primarySocket.send(ans)
        tempPort = primarySocket.recv(bufferSize)
        print('Ephermeral port # ', tempPort)
        success = uploadFileToServer(fileName, tempPort)
        if success == 0:
            print('fail to upload')
        else:
            print('Finish uploading')
    elif command == 'get' and ftp_arg_count == 1:
        fileName = rest
    elif command == 'ls' and ftp_arg_count == 0:
        primarySocket.send(ans)
        print('sending ls request to server')
        ephPort = primarySocket.recv(bufferSize)
        print('Ephermeral port # ', ephPort)
        ephSocket = createSocket(ephPort)
        data = ephSocket.recv(bufferSize)
        server_dir = pickle.loads(data)
        for line in server_dir:
            print(line)
        ephSocket.close()
    elif command == 'quit' and ftp_arg_count == 0:
        primarySocket.send(ans)
        print('closing now')
        primarySocket.close()
    else:
        print('not a valid command')

import sys
import os
import subprocess
import socket
import pickle
bufferSize = 4096
serverName = 'localhost'


def recvAll(sock, numBytes):
    recvBuff = ''
    while len(recvBuff) < numBytes:
        tmpBuff = sock.recv(numBytes)
        if not tmpBuff:
            break
        recvBuff += tmpBuff
    return recvBuff


def createSocket(portNum):
    connSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    connSock.connect((serverName, int(portNum)))
    print('Connected to port #', portNum)
    return connSock


def sizeOfFile(fileName):
    statinfo = os.stat(fileName)
    size = statinfo.st_size
    return size


def uploadFileToServer(fileName, portNumb):
    tempSocket = createSocket(portNumb)
    try:
        file_object = open(fileName, 'r')
    except OSError:
        print('can not open file %s to read', fileName)
        tempSocket.close()
    fileSize = sizeOfFile(fileName)
    print('Uploading', fileName, 'to server')
    while True:
        fileData = file_object.read(fileSize)
        if fileData:
            dataSizeStr = str(len(fileData))
            while len(dataSizeStr) < 10:
                dataSizeStr = '0' + dataSizeStr
            fileData = dataSizeStr + fileData
            numSent = 0
            while len(fileData) > numSent:
                numSent += tempSocket.send(fileData[numSent:])
        else:
            break
        print('Sent ', numSent, ' bytes.')
    file_object.close()
    tempSocket.close()


if len(sys.argv) < 3:
    print('python ' + sys.argv[0] + '<server_machine>' + '<server_port>')
serverName = sys.argv[1]
serverPort = int(sys.argv[2])
primarySocket = createSocket(serverPort)
ans = ''
fileName = ''
success = ''
while ans != 'quit':
    ans = raw_input('ftp> ')
    ftp_arg_count = ans.count(' ')
    if ftp_arg_count == 1:
        command, rest = ans.split()
    elif ftp_arg_count == 0:
        command = ans
    if command == 'put' and ftp_arg_count == 1:
        fileName = rest
        primarySocket.send(ans)
        tempPort = primarySocket.recv(bufferSize)
        print('Ephermeral port # ', tempPort)
        success = uploadFileToServer(fileName, tempPort)
        if success == 0:
            print('fail to upload')
        else:
            print('Finish uploading')
    elif command == 'get' and ftp_arg_count == 1:
        fileName = rest
    elif command == 'ls' and ftp_arg_count == 0:
        primarySocket.send(ans)
        print('sending ls request to server')
        ephPort = primarySocket.recv(bufferSize)
        print('Ephermeral port # ', ephPort)
        ephSocket = createSocket(ephPort)
        data = ephSocket.recv(bufferSize)
        server_dir = pickle.loads(data)
        for line in server_dir:
            print(line)
        ephSocket.close()
    elif command == 'quit' and ftp_arg_count == 0:
        primarySocket.send(ans)
        print('closing now')
        primarySocket.close()
    else:
        print('not a valid command')

# *******************************************************************
# Trang Nguyen trang_nguyen@csu.fullerton.edu CWID:802816165
# Insert your name here
# 
# CPSC 471 - Section 2
# *******************************************************************

import sys
import os
import subprocess
import socket
import pickle #for receiving list data

bufferSize = 4096
serverName = "localhost"

# *******************************************************************
#							FUNCTIONS
# We need to write these functions:
# a) Receives the specified number of bytes from a specified socket
# b) Gets size of a file in bytes
# c) Upload a file to server
# d) Download a file from server
# e) List file from the server
# f) quit (disconnect/exit)
# *******************************************************************


def recvAll(sock, numBytes):

	# The buffer
	recvBuff = ""
	
	# Keep receiving till all is received
	while len(recvBuff) < numBytes:
		# temporary buffer
		# Attempt to receive bytes
		tmpBuff =  sock.recv(numBytes)
		
		# The other side has closed the socket
		if not tmpBuff:
			break
		
		# Add the received bytes to the buffer
		recvBuff += tmpBuff
	
	return recvBuff

# Function to create a socket using a provided port number
def createSocket(portNum):

	# Create a TCP socket
	connSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	
	# Connect to server
	connSock.connect((serverName, int (portNum)))
	print ("Connected to port #", portNum)
	
	# Return a socket
	return connSock

	
def sizeOfFile (fileName):
	statinfo = os.stat(fileName)
	size = statinfo.st_size
	return size

def uploadFileToServer(fileName, portNumb):

	#generate an emphemeral port
	tempSocket=createSocket(portNumb)

	#open file 
	try:
		file_object=open(fileName,'r')
	except OSError:
		print ("can not open file %s to read", fileName)
		tempSocket.close()

	fileSize=sizeOfFile(fileName)
	print ("Uploading", fileName,"to server")

	while True:
		# Read  data
		fileData = file_object.read(fileSize)
	
		# 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 += tempSocket.send(fileData[numSent:])
	
		# The file has been read. We are done
		else:
			break

		print ("Sent ", numSent, " bytes.")
	
	# Close the socket and the file
	file_object.close()
	tempSocket.close()
	
	

#def downloadFileFromServer(socket,fileName):





# *******************************************************************
#							MAIN PROGRAM
# *******************************************************************


#if client command line has 3 args. for ex: python client.py localhost 1234

if len(sys.argv) < 3:
	print ("python " + sys.argv[0]+"<server_machine>"+"<server_port>")

serverName=sys.argv[1]
serverPort=int (sys.argv[2])

primarySocket= createSocket(serverPort)

ans = ""
fileName = ""
success = ""

while (ans != "quit"):
	ans =raw_input("ftp> ")

	#argument counting with spaces
	ftp_arg_count = ans.count(' ')
	if ftp_arg_count == 1:
		(command, rest) = ans.split()
	elif ftp_arg_count == 0:
		command = ans

	if command == "put" and ftp_arg_count == 1:
		fileName = rest
		primarySocket.send(ans)
		tempPort=primarySocket.recv(bufferSize)
		print ("Ephermeral port # ",tempPort)
		success = uploadFileToServer(fileName,tempPort)
		if success == 0:
			print ("fail to upload")
		else:
			print("Finish uploading")
	elif command == "get" and ftp_arg_count == 1:
		fileName = rest
	elif command == "ls" and ftp_arg_count == 0:
		#NOTE: not sure how buffer works so just used bufferSize for the recieves -andre

		primarySocket.send(ans)
		print("sending ls request to server")

		#get ephemeral port generated by server
		ephPort = primarySocket.recv(bufferSize)
		print ("Ephermeral port # ",ephPort)

		#create ephemeral socket and wait for data
		ephSocket = createSocket(ephPort)
		data = ephSocket.recv(bufferSize)

		#need 'pickle.loads' to extract list
		server_dir = pickle.loads(data)

		#print out directory
		for line in server_dir:
			print(line)

		ephSocket.close()

	elif command == "quit" and ftp_arg_count == 0:
		primarySocket.send(ans)
		print ("closing now")
		primarySocket.close()
	else:
		print ("not a valid command")

		







