def returnBook(self): # return book to libaray server # send memo xml file to data server try: sock = socket.create_connection(self.server_address) content = '' logging.debug(self.book_title) with open(here("clientdata/" + self.book_title + ".xml"), "r") as xmlfd: for r in xmlfd: content += r f = sock.makefile("rwb", 0) data = Protocol.ReturnRequest(book_title=self.book_title, memo_content=content, user_id=self.user_data.userId) f.write(data.serialize()) rdata = sock.recv(1024) recv_data = Protocol.ReturnRequest.parse(rdata) logging.debug("return " + bytes.decode(recv_data.memo_content)) os.remove(here("clientdata/" + self.book_title + ".pdf")) self.setParent(None) except IOError as e: logging.debug(e) except Exception as e: logging.debug(e) else: f.close() sock.close() pass
def data_request(self, data): temp = bytes.decode(data.command).split('&') uid = temp[1] certkey = bytes.decode(data.cert_key) logging.debug(temp) con = None d_certkey = None try: con = sqlite3.connect(DATABASE) with con: cur = con.cursor() cur.execute("SELECT certkey FROM USERS WHERE id=:id", {"id": uid}) # con.commit() row = cur.fetchone() cur.close() if (row): d_certkey = row[0] # print("certkey:",certkey) except sqlite3.Error as e: print(e) if con: con.rollback() else: if (certkey == d_certkey): print("SUCCESS!") # Success! try: with con: scur = con.cursor() scur.execute( """ SELECT bd.title,bd.file_path FROM BOOK bd NATURAL JOIN borrowbook br WHERE br.book_id=bd.book_id and br.user_id=:id; """, {'id': uid}) rows = scur.fetchall() logging.debug((rows[0], rows[0][0])) except Exception as e: logging.debug(e) else: # how many books data.file_count = len(rows) # each books data size try: for i, r in zip(range(len(rows)), rows): file_info = os.stat(here(r[1])) img_file_info = os.stat( here("data/" + r[0] + ".jpg")) data.file_name[i] = r[0] data.file_size[i] = file_info.st_size data.img_file_size[i] = img_file_info.st_size # self.temp_dict[certkey].append((r[0],r[1])) except Exception as e: logging.debug(e) else: self.reply(data) try: for r in rows: with open(here(r[1]), "rb") as f: l = f.read(1024) # for line in f: while (l): # temp = f.read(1024) # logging.debug(temp) self.reply(l) l = f.read(1024) # sleep(1) for r in rows: with open(here("data/" + r[0] + ".jpg"), "rb") as f: l = f.read(1024) # for line in f: while (l): # temp = f.read(1024) # logging.debug(temp) self.reply(l) l = f.read(1024) except Exception as e: logging.debug(e) try: for r in rows: content = "" m = Protocol.ReturnRequest() with open(here('data/memo/' + r[0] + ".xml"), "r") as f: for l in f: content += l m.memo_content = content self.reply(m) except Exception as e: logging.debug(e) return else: data.command = Protocol.FAIL_MSG logging.debug(data) return data