diff --git a/networker.py b/networker.py index 06a91c5..8946d78 100644 --- a/networker.py +++ b/networker.py @@ -15,25 +15,30 @@ class Message: self.header = header if mtype == MTYPE_File: try: - f = open(header, "r") + f = open(header, "rb") try: - self.content = str(f.read()) + self.content = f.read() except: print("An issue writing the message for \"" + self.header + "\" occured.") f.close() except: print("An issue when opening a file for reading: \"" + self.header + "\" occured.") + #print(traceback.format_exc()) else: self.content = content def saveContent(self): if self.mtype != MTYPE_File: pass try: - f = open(str(self.header), "w") + f = open(str(self.header), "wb") try: - f.write(bytes(self.content)) + if type(self.content) == bytes or type(self.content) == bytearray: + f.write(bytes(self.content)) + else: + f.write(bytes(self.content, encoding='utf-8')) except: print("An issue writing the message for \"" + str(self.header) + "\" occured.") + #print(traceback.format_exc()) f.close() except: print("An issue when opening a file for writing: \"" + str(self.header) + "\" occured.") @@ -42,7 +47,7 @@ class Message: toReturn = {"mtype":self.mtype, "header":self.header, "ident__":"Message"} if type(self.content) == bytes or type(self.content) == bytearray: toReturn["contentb64"] = True - toReturn["content"] = base64.b64encode(bytes(self.content)).decode() + toReturn["content"] = base64.b64encode(bytes(self.content)).decode('utf-8') else: toReturn["contentb64"] = False toReturn["content"] = self.content