Exemplo n.º 1
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()
Exemplo n.º 2
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()
Exemplo n.º 3
0
def main(argv):
    serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    serverSocket.bind((HOST, PORT))
    serverSocket.listen(1)
    print 'Server setup done'
    while (True):
        connectionSocket, addr = serverSocket.accept()
        fileName = Lib.readTextTCP(connectionSocket)
        fileSize = Lib.check_File_Exists(fileName)
        sendFile(fileName, fileSize, connectionSocket)
        connectionSocket.close()
Exemplo n.º 4
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")
Exemplo n.º 5
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
Exemplo n.º 6
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()
Exemplo n.º 7
0
def main(argv):
	# TO DO Your Code
    serverSocket = socket(AF_INET,SOCK_STREAM)
    serverSocket.bind(('', PORT))
    serverSocket.listen(1)
    print 'The server is ready to receive'
    
    while 1:
        connectionSocket, addr = serverSocket.accept()
        file = Lib.readTextTCP(connectionSocket)
        filename = Lib.extractFilename(file)
        size = str(Lib.check_File_Exists(filename))
        sendFile(filename, size, connectionSocket)
        
    serverSocket.close()
Exemplo n.º 8
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()
Exemplo n.º 9
0
def receiveFile(fileName, size, conn):
	recievedData = 0
	with open(fileName, 'wb') as file:
		print "RECEIVING - File lader sig fylde af dejlig data."
		
		while True:
			modtag = Lib.readTextTCP(conn)
			if modtag == '0':
				break
			recievedData = recievedData + int(modtag)
			print "Modtaget:", recievedData, "B /", size, "B"
			data = conn.recv(int(modtag))
			file.write(data)
	
	file.close()
	print "COMPLETE - Filen er fyldt af modtaget data og derfor lukket."
Exemplo n.º 10
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()
Exemplo n.º 11
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()
Exemplo n.º 12
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()
Exemplo n.º 13
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()
Exemplo n.º 14
0
def printServerResponse(conn):
    serverResponse = Lib.readTextTCP(conn)
    print('From Server:', serverResponse.decode())
Exemplo n.º 15
0
def printConnectionEstablished(conn):
    receivedMessage = Lib.readTextTCP(conn)
    print(receivedMessage)
    Lib.writeTextTCP('Server is ready to recieve file name', conn)
Exemplo n.º 16
0
def getTargetFile(conn):
    targetFileName = ""
    targetFileName = Lib.readTextTCP(conn)
    print('Extracting:')
    print(targetFileName)
    return targetFileName