def receive(sdef, slen): sdef.setblocking(0) ready = select.select([sdef], [], [], 60) if ready[0]: data = int(sdef.recv(slen)) # receive length #print ("To receive: {}".format(data)) else: raise RuntimeError("Socket timeout") chunks = [] bytes_recd = 0 while bytes_recd < data: ready = select.select([sdef], [], [], 60) if ready[0]: chunk = sdef.recv(min(data - bytes_recd, 2048)) if chunk == b'': raise RuntimeError("Socket connection broken") chunks.append(chunk) bytes_recd = bytes_recd + len(chunk) else: raise RuntimeError("Socket timeout") segments = b''.join(chunks).decode("utf-8") #print ("Received segments: {}".format(segments)) return picklemagic.safe_loads(ast.literal_eval(segments))
def receive(sdef, slen): sdef.setblocking(0) ready = select.select([sdef], [], [], 240) if ready[0]: try: data = int(sdef.recv(slen)) # receive length #print ("To receive: {}".format(data)) except: raise RuntimeError("No data received, probably probing") #do away with the invalid literal for int else: raise RuntimeError("Socket timeout") chunks = [] bytes_recd = 0 while bytes_recd < data: ready = select.select([sdef], [], [], 240) if ready[0]: chunk = sdef.recv(min(data - bytes_recd, 2048)) if not chunk: raise RuntimeError("Socket connection broken") chunks.append(chunk) bytes_recd = bytes_recd + len(chunk) else: raise RuntimeError("Socket timeout") #print ("Received segments: {}".format(segments)) try: segments = b''.join(chunks).decode("utf-8") return json.loads(segments) except: #compatibility segments = b''.join(chunks).decode("utf-8") return picklemagic.safe_loads(ast.literal_eval(segments))
import base64, picklemagic with open("logo.gif", "rb") as imageFile: str = base64.b64encode(imageFile.read()) print(len(str)) with open("logo.gif", "rb") as imageFile: str2 = picklemagic.safe_dumps(imageFile.read()) print(len(str2)) print(picklemagic.safe_loads(str2)) #DEMO FOLLOWS from tkinter import * root = Tk() # logo frame = Frame(root, height=100, width=100) frame.grid(row=0, column=1, sticky=W + E + N) logo_hash_decoded = base64.b64decode(str) logo = PhotoImage(data=logo_hash_decoded) logo_hash_decoded2 = picklemagic.safe_loads(str2) logo2 = PhotoImage(data=logo_hash_decoded) image = Label(frame, image=logo).grid(pady=25, padx=50, sticky=N) image2 = Label(frame, image=logo2).grid(pady=25, padx=50, sticky=N) # logo root.mainloop()