Ejemplo n.º 1
0
def sendFile(fileName,  fileSize,  conn):
	Lib.writeTextTCP(str(fileSize), conn)
	file = open(fileName, 'rb')
	data = file.read(BUFSIZE)
	while data:
		conn.send(data)
		data = file.read(BUFSIZE)
Ejemplo n.º 2
0
def main(argv):
    serverSocket = socket(AF_INET,SOCK_STREAM)
    serverSocket.bind((HOST, PORT))
    serverSocket.listen(1)
    
    while 1:
        print("The server is ready to accept...")
        connectionSocket, addr = serverSocket.accept()
        print("Socket accept", addr)
        
        fileName = Lib.readTextTCP(connectionSocket) #Modtager sti og filnavn fra client
        #fileName2 = Lib.extractFilename(fileName)
        print("Message received from client:", fileName)
        
        fileExists = Lib.check_File_Exists(fileName)
        fileExists2 = str(fileExists)
        #if  fileExists != 0: #returns fileSize
            #file = open(fileName2,'rb') #opens file without truncation
            #file2 = file.read(all)
        #fileSize = Lib.getFileSizeTCP(connectionSocket)
        Lib.writeTextTCP(fileExists2, connectionSocket)
    
        sendFile(fileName, fileExists, connectionSocket)
        #else:
            #Lib.writeTextTCP("File not found", connectionSocket)    #Sender error message til client
    connectionSocket.close()
Ejemplo n.º 3
0
def main(argv):
    if len(argv) > 2:
        SERVER = sys.argv[1]
    else:
        print("Benyt formatet 'python file_client.py <IP>'\n")
        sys.exit()
    sock = socket(AF_INET, SOCK_STREAM)
    sock.connect((SERVER, PORT))
    print("Forbundet til server\n")
    cmd = input("Indtast filnavn. Ved afslutning skriv 'exit'\n")
    while cmd != "exit":
        Lib.writeTextTCP(cmd, sock)

        fileSize = Lib.getFileSizeTCP(sock)
        print(fileSize)

        if fileSize > 0:
            receiveFile(cmd, sock, fileSize)
        else:
            print("Fil ikke fundet\n")
        cmd = input("Indtast filnavn. Ved afslutning skriv 'exit'\n")

    Lib.writeTextTCP(cmd, sock)
    sock.close()
    sys.exit()
Ejemplo n.º 4
0
def requestFile(file, conn):
    print('Requesting file')
    Lib.writeTextTCP(file, conn)
    fileSize = Lib.getFileSizeTCP(conn)
    print('Size is: ', fileSize)
    print('Requesting,', file)
    Lib.writeTextTCP('Ready to receive', conn)
    return fileSize
Ejemplo n.º 5
0
def main(argv):
    #creating an INET an streaming socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    #bind to the HOST id and port 9000
    sock.bind((HOST, PORT))

    #The server can queue up to 5 connect requests
    sock.listen(5)

    print("Server socket opened")

    while True:
        #accepts connection from client
        clisock, addr = sock.accept()

        print("connection from {} achieved".format(addr))

        #reads the message from the client
        msg = Lib.readTextTCP(clisock)

        print("received following message: {} ".format(msg))

        #if the message is close it will close the connections and exit
        if msg == "close":
            sock.close()
            print("Server socket closed")
            clisock.close()
            sys.exit()

        #checks if the file exists and returns the size of the file, if it doesn't exist it returns 0
        size = Lib.check_File_Exists(msg)

        if size > 0:
            print("The size of the file is: {} bit".format(size))
            #converts the size of the file to a string and sends it to the client
            returnmsg = str(size)
            Lib.writeTextTCP(returnmsg, clisock)

            #opens the requested file
            file = open(msg, 'rb')

            #reads BUFSIZE bits from the file
            filedata = file.read(BUFSIZE)

            #while there is data to be read it will send the data and read new data
            while filedata:
                clisock.send(filedata)
                filedata = file.read(BUFSIZE)

        else:
            print("The file doesn't exist")
            #converts the size of the file to a string and sends it to the client
            returnmsg = str(size)
            Lib.writeTextTCP(returnmsg, clisock)

        clisock.close()
Ejemplo n.º 6
0
def main(ip, file):
    print('ip: ', ip)
    print('file: ', file)
    clientSocket = socket(AF_INET, SOCK_STREAM)
    clientSocket.connect((ip, SERVERPORT))
    Lib.writeTextTCP('Client initiated TCP connection', clientSocket)
    printServerResponse(clientSocket)
    sizeOfFile = int(requestFile(file, clientSocket))
    receiveFile(file, clientSocket, sizeOfFile)
    clientSocket.close()
Ejemplo n.º 7
0
def main(argv):
    fileName = sys.argv[1]  #input from terminal when compiling
    clientSock = socket(AF_INET, SOCK_STREAM)  #create tcp socket
    clientSock.connect((HOST, PORT))  #connect socket to the server address

    print("Connected to server")

    Lib.writeTextTCP(fileName, clientSock)  #writes filename to server

    receiveFile(fileName, clientSock)  #calls recieveFile()
Ejemplo n.º 8
0
def receiveFile(fileName, conn):
    Lib.writeTextTCP(fileName, conn)
    filelength = int(float(Lib.readTextTCP(conn)))
    print "fil length er {}".format(filelength)

    chunks = int((filelength // BUFSIZE) + 1)
    f = open("billede.jpg", "wb")
    for i in range(0, chunks):
        content = conn.recv(BUFSIZE)
        f.write(content)
        print 'Received a package'

    print 'Done downloading'
    f.close
Ejemplo n.º 9
0
def receiveFile(fileName, conn):
    Lib.writeTextTCP(fileName, conn)
    fileSize = Lib.getFileSizeTCP(conn)
    if fileSize != 0:
        fileHandle = open(fileName, "wb")
        toReceive = fileSize

        while toReceive > 0:
            received = conn.recv(min(toReceive, BUFSIZE))
            fileHandle.write(received)
            toReceive -= len(received)
        print "Finished getting file"
        fileHandle.close()
    else:
        print "File '" + fileName + "' did not exist on the server"
Ejemplo n.º 10
0
def main(argv):
    serverSock = socket(AF_INET, SOCK_STREAM)  #create TCP socket
    serverSock.bind((HOST, PORT))  #Bind socket to IP og port(address)
    serverSock.listen(1)  #waiting for client to connect.
    print 'The server is ready to receive'

    while True:
        conn, addr = serverSock.accept()  #completes connection with client
        print 'Connected to client - Port - ', PORT
        txtClient = Lib.readTextTCP(
            conn)  #Reads file and directory from client
        fileName = Lib.extractFilename(txtClient)
        fileSize = str(Lib.check_File_Exists(fileName))
        Lib.writeTextTCP(fileSize, conn)  #send filesize to client.
        sendFile(fileName, fileSize, conn)  #calls sendfile()
Ejemplo n.º 11
0
def sendFile(fileName, fileSize, conn):
    Lib.writeTextTCP(str(fileSize), conn)

    if fileSize != 0:
        fileHandle = open(fileName, "rb")
        toSendBytes = fileSize
        print "Sending file: " + fileName
        print "With size: " + str(fileSize)

        while toSendBytes > 0:
            toSend = fileHandle.read(BUFSIZE)
            toSendBytes -= len(toSend)
            conn.send(toSend)

        print "Finished sending..."
        fileHandle.close()
Ejemplo n.º 12
0
def getFile(fileName, conn):
	# TO DO Your Code
    print("Inside receiveFile")
    Lib.writeTextTCP(fileName, conn)
   
    print("After conn.sendto")
    #sendto(data: bytes, address: _Addresss)
    #bytesAddresPair = conn.recvfrom(2048)
    #number of bytes to be read from the TCP socket
    sizeOfFile = int(Lib.readTextTCP(conn))
    print(sizeOfFile)
    print("after sizeOfFile")
    if sizeOfFile == 0 :
        print("Der findes ikke nogen fil")
        return
    fileNameExtracted = Lib.extractFilename(fileName)
    fd = open(fileNameExtracted, 'wb')
   
#    while sizeOfFile >= BUFSIZE:
#        print("Inside receiveFile Inside while loop")
#        var = Lib.readImageTCP(conn)
#        fd.write(var)
#        
#        sizeOfFile -= BUFSIZE
#        print("After fileLEngth -=")
#    print("fd.close") 
#    var = Lib.readImageTCP(conn)
    i = 1
    #databuf = b""
    while sizeOfFile > 0:
        
        print("receiving datapacket: ", i)
        if sizeOfFile >= 1000:
            data = conn.recv(BUFSIZE)
            #databuf += data
            sizeOfFile -= BUFSIZE
            i += 1
            print("data received: ", data)
        
        elif sizeOfFile < BUFSIZE:
            data = conn.recv(sizeOfFile)
            print("data received: ", data)
            sizeOfFile -= sizeOfFile
        fd.write(data)
            
            
    fd.close()
Ejemplo n.º 13
0
def main(argv):
    # TO DO Your Code

    # Create a TCP/IP socket
    serverSocket = socket(AF_INET, SOCK_STREAM)

    # Bind the socket to the port
    server_address = (HOST, PORT)
    serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    serverSocket.bind(server_address)

    # Listen for incoming connections
    serverSocket.listen(1)

    print("Server are ready to recieve")

    # loop waiting for connections (terminate with Ctrl-C)
    try:
        while True:
            connectionSocket, address = serverSocket.accept()
            print "Connected from", address
            sentence = Lib.readTextTCP(connectionSocket)
            print "Client beder om: ", sentence
            file_name = Lib.extractFilename(sentence)
            print "Filnavn: ", file_name
            file_size = Lib.check_File_Exists(sentence)

            if file_size != 0:
                print "Filen findes!"
                print "Filen fylder: ", file_size

                Lib.writeTextTCP(str(file_size), connectionSocket)

                sendFile(sentence, file_size, connectionSocket)
                print "Filen er sendt!"

            else:
                print "Kuk kuk - tom fil eller er den bare tom?"
                Lib.writeTextTCP("Ingen fil..", connectionSocket)

            connectionSocket.close()
            print "Disconnected from", address

    finally:
        serverSocket.close()
Ejemplo n.º 14
0
def main(argv):
    # TO DO Your Code
    if len(argv) > 0:  #give arguments in the terminal names
        TARGET = argv[0]
        FILE = argv[1]
    else:
        sys.exit()

    client = socket(AF_INET, SOCK_STREAM)  #making the socket
    client.connect((TARGET, PORT))

    Lib.writeTextTCP(FILE, client)  #asking for file
    sizer = Lib.readTextTCP(client)  #receives the filesize from server

    receiveFile(FILE, client, sizer)  #receive the file

    client.close()
    sys.exit()
Ejemplo n.º 15
0
def main(argv):
    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serverSocket.bind(('', PORT))
    serverSocket.listen(1)
    print('The server is running and ready to receive')

    while True:
        connectionSocket, addr = serverSocket.accept()
        printConnectionEstablished(connectionSocket)
        print('Waiting to recieve file name')
        fileName = ''
        fileSize = ''
        fileName = getTargetFile(connectionSocket)
        fileSize = getTargetFileSize(fileName)
        Lib.writeTextTCP(str(fileSize), connectionSocket)
        sendFile(fileName, fileSize, connectionSocket)
        time.sleep(1)
        connectionSocket.close()
Ejemplo n.º 16
0
def sendFile1(file,  fileSize,  conn):
	# TO DO Your Code
        
    Lib.writeTextTCP(fileSize, conn)

    if fileSize > BUFSIZE:
        sizebuf = fileSize
        i = 1000
        j = i-1000

        while sizebuf > 0:
            if sizebuf > 1000:
                conn.sendFile(file[j:i],conn)
                sizebuf -= i
                i += 1000

            elif sizebuf < 1000:
                conn.sendFile(file[sizebuf],conn)
    conn.close
Ejemplo n.º 17
0
def main(argv):
    clisock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    #checks if input is correct
    if len(argv) == 2:
        serv = argv[0]
        msg = argv[1]
    else:
        print("Formatet skal vaere <server IP> <sti/Filnavn>")
        sys.exit()

    #client connects to server
    clisock.connect((serv, PORT))
    print("Connection til server etableret")

    #writes the filename t the server
    Lib.writeTextTCP(msg, clisock)

    #gets the filsize
    size = Lib.getFileSizeTCP(clisock)
    print("filstorelsen er: {}".format(size))
    dataread = 0

    #checks if the file exsists and if it does it starts receiving the file
    if size > 0:
        filename = Lib.extractFilename(msg)
        print("Filnavnet er: {}".format(msg))

        file = open(filename, 'wb')

        while dataread < size:
            filedata = clisock.recv(BUFSIZE)
            file.write(filedata)
            dataread = dataread + len(filedata)

        print("filen er faerdigoverfort")

    else:
        print("File not found on server")
        sys.exit()

    clisock.close()
Ejemplo n.º 18
0
def main(argv):
    #Connect to socket
    s = socket(AF_INET,SOCK_STREAM)
    print("The client is connecting...")
    s.connect((serverName, PORT))
    print ("Client Connected")
    
    filename = input("Filename? -->")   #Enter filename
    print(filename)
    Lib.writeTextTCP(filename,s)
    
    #filesize = Lib.getFileSizeTCP(filename) #Get filesize
    #print(filesize)
    
    if filename == 'q': 
        s.close()
    else:
        #getFile(filename, filesize, s)
        getFile(filename,s)
    s.close()
Ejemplo n.º 19
0
def sendFile(fileName,  fileSize,  conn):
	#fileName = Lib.extractFilename(fileName)

	i = 0
	text = "."

	#Sending size of requested file
	Lib.writeTextTCP(str(fileSize), conn)

	with open(fileName, "rb") as file_obj:
		while 1: #text not == "":
			text = file_obj.read(BUFSIZE)
			i = i + 1
			if text == "":
				conn.send(text)		
				break
			conn.send(text)
			print 'Packets sent: ', i

	print 'File sent: ', fileName
Ejemplo n.º 20
0
def main(argv): 
	try:
		#Initiate server
		serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #INET equals streaming socket equals TCP
		
		#Prevent socket from blocking
		#serversocket.setblocking(0)
		serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

		#Bind socket to PORT and localhost
		serversocket.bind((HOST,PORT)) 
		serversocket.listen(1) #We want to queue up to just one client
		print 'Socket connection on: ', serversocket.getsockname()

	except socket.error as msg:
		print 'Error connecting with serversocket: %s\n Terminating program.' %msg
		serversocket.close()
		sys.exit()
	
	while(1):
		#Wait for connections
		(clientsocket, address) = serversocket.accept()
		print 'Incomming connection from', address

		filename = Lib.readTextTCP(clientsocket) #Connect with client
		print '1 ', filename
	
		filesize = Lib.check_File_Exists(filename) #Returns size
		print '2 filesize ', filesize
	
		if filesize != 0:
			sendFile(filename, filesize, clientsocket)
		else: 
			Lib.writeTextTCP("0", clientsocket)	
			Lib.writeTextTCP("File " + filename + "doesnt exist.", clientsocket)			
	
		print '3 Closing connection ', address
		clientsocket.close()
Ejemplo n.º 21
0
def main(argv):

	
	serverSocket = socket(AF_INET, SOCK_STREAM)
	serverSocket.bind((LOCALIP, PORT))
	serverSocket.listen(1) 
	print("The server is ready to recieve\n")
	while True:
		connectionSocket, addr = serverSocket.accept()
		print("client connected\n")
		msg = Lib.readTextTCP(connectionSocket)
		while msg != "exit":
			print("Command received: %s\n" % msg)
			fileSize = Lib.check_File_Exists(msg)
	
			if fileSize > 0:
				
				sendFile(msg, fileSize, connectionSocket)
			else:
				Lib.writeTextTCP(str(fileSize), connectionSocket)
			msg = Lib.readTextTCP(connectionSocket)
		connectionSocket.close()
		print("Connection closed")
Ejemplo n.º 22
0
def main(argv):
	host = sys.argv[1]
	file = sys.argv[2]

	# Create a TCP/IP socket
	clientSocket = socket(AF_INET, SOCK_STREAM)
	server_address = (host, PORT)
	clientSocket.connect(server_address)

	fileName = Lib.extractFilename(file)
	
	Lib.writeTextTCP(file,clientSocket)

	fileSize = Lib.readTextTCP(clientSocket)
	fileSize = int(fileSize)
	print fileSize

	msg = Lib.readTextTCP(clientSocket)
	if msg == "Sender Fil...":
		receiveFile(fileName, fileSize, clientSocket)
	else:
		print "Fejl 40 -", msg

	clientSocket.close()
Ejemplo n.º 23
0
def sendFile(fileName, fileSize, conn):
    # TO DO Your Code
    Lib.writeTextTCP("Sender Fil...", conn)
    filepoint = 1
    file = open(fileName, 'rb')
    sizeLeft = fileSize

    while (sizeLeft > 0):
        if sizeLeft < BUFSIZE:
            Lib.writeTextTCP(str(sizeLeft), conn)
            filepoint = file.read(sizeLeft)

        else:
            Lib.writeTextTCP(str(BUFSIZE), conn)
            filepoint = file.read(BUFSIZE)

        print "Sender...."
        conn.send(filepoint)
        sizeLeft = sizeLeft - BUFSIZE

    Lib.writeTextTCP('0', conn)
    file.close()
Ejemplo n.º 24
0
def sendFile(fileName,  fileSize,  conn):
    Lib.writeTextTCP(fileSize, conn)
    with open(fileName, "rb") as f:
        conn.send(f.read())
    conn.close()
    f.close()
Ejemplo n.º 25
0
def printConnectionEstablished(conn):
    receivedMessage = Lib.readTextTCP(conn)
    print(receivedMessage)
    Lib.writeTextTCP('Server is ready to recieve file name', conn)