class Book(): def __init__(self): self.lib = Lib() self.bookdb = Bookdb() def get_post_book(self): vdict = copy.deepcopy(config.vdict) vdict['books'] = self.bookdb.select(vdict['book_max_post']) vdict['thumbs'] = self.lib.get_thumbs(vdict['books'], 2) return vdict def get_book(self, id): vdict = copy.deepcopy(config.vdict) vdict['book'] = self.bookdb.select(1, id=id) return vdict def load(self, page): vdict = copy.deepcopy(config.vdict) vdict['books'] = self.bookdb.select(vdict['book_max_post'], page=page) vdict['thumbs'] = self.lib.get_thumbs(vdict['books'], 2) if vdict['books']: new_list = [] for book in vdict['books']: new_book = list(book) new_book[3] = book[3].strftime('%d/%m/%Y') new_book[4] = book[4].strftime('%H:%M:%S') new_list.append(new_book) vdict['books'] = new_list return vdict
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)
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()
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()
class Post(): def __init__(self): self.postdb = Postdb() self.lib = Lib() def get_post(self, amount=5, category="", page=0): vdict = copy.deepcopy(config.vdict) vdict['posts'] = self.postdb.select(amount=amount, category=category, page=page) vdict['thumbs'] = self.lib.get_thumbs(vdict['posts'], 3) vdict['videos'] = self.lib.get_video_data(vdict['posts'], 7) vdict['category'] = category new_post_list = [] for post in vdict['posts']: new_post = list(post) new_post[4] = post[4].strftime('%d/%m/%Y') new_post[5] = post[5].strftime('%H:%M:%S') new_post_list.append(new_post) vdict['posts'] = new_post_list return vdict def get_single_post(self, id): vdict = copy.deepcopy(config.vdict) vdict['post'] = self.postdb.select(1, id=id) return vdict
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
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()
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()
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()
def __init__(self, log_level='DEBUG'): """ Library for compressImages.py interaction. Args: log_level (str): Logging level setting ie: "DEBUG" or "WARN" """ self.__log_level = log_level self.__compress_images_obj = CompressImage() self.__deleteBackupImageObj = DeleteBackupImage() self.__lib = Lib(log_level=self.__log_level)
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
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()
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()
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"
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()
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()
def __init__(self): self.lib = Lib() self.postdb = Postdb() self.categorydb = Categorydb() self.pagedb = Pagedb() self.bookdb = Bookdb() self.userdb = Userdb() self.settingdb = Settingdb()
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()
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()
def receiveFile(nm, conn, siz): bytesRead = 0 fileName = Lib.extractFilename(nm) file = open(fileName, 'wb') while bytesRead < siz: buff = conn.recv(BUFSIZE) file.write(buff) bytesRead = bytesRead + len(buff) print("Transfer complete\n")
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
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()
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()
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
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")
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()
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()
def receiveFile(fileName, conn, size): # TO DO Your Code bytesToRead = 0 name = Lib.extractFilename(fileName) #getting the name of the file file = open(name, 'wb') #using the file name to make a new file while (int(size) > bytesToRead): #building the new file data = conn.recv(BUFSIZE) file.write(data) bytesToRead += len(data) if bytesToRead == int( size): # makes sure that the file doesnt get too much data break file.close() print("completed transfering file")
def sendFile(fileName, clientAddr, server): try: # send size af fil msg = "File size: " + str(Lib.check_File_Exists(fileName)) server.sendto(msg.encode(), clientAddr) except: # send fejl besked server.sendto("File not found!".encode(), clientAddr) with open(fileName, "rb") as file: # læs fra filen data = file.read(HEADER) # læs første 1000 bytes print("Sending...") while data: # send mens der er data tilbage server.sendto(data, clientAddr) data = file.read(HEADER) print("File sent.")
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."
# import lib # # l = lib.Lib() # l.sayHi() from lib import Lib l = Lib() l.sayHi()