Object Transmission in Python using Pickle [duplicate]

2024/9/20 17:44:39

I have the following class, a Point object

class Point:def __init__(self):passdef __init__(self, x, y):self.x = xself.y = y

And I have a server (Uses UDP)

# Server side
import socket
import picklehost = "localhost"
port = 10000s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))data = s.recvfrom(1024)
print(data)

And my client side is:

import socket
import pickle
from Point import *host = "localhost"
port = 10000
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)p = Point(10, 20)
a = pickle.dumps(p)s.sendto(a, (host, port))

In the server side, whenever I get the p and print it, I get the following (b'\x80\x03cPoint\nPoint\nq\x00)\x81q\x01}q\x02(X\x01\x00\x00\x00xq\x03K\nX\x01\x00\x00\x00yq\x04K\x14ub.', ('127.0.0.1', 55511))

How can I get the object, instead of this?

Answer

First, a caveat. Pickles allow for arbitrary code execution. Do not use this to accept arbitrary connections, and preferably use cryptography to ensure you are only exchanging trusted data. Even then, consider using a safer exchange format.

Next, take into account that UDP packets are limited in size. You need to make sure your pickle data is small enough to fit in a UDP packet (the maximum payload size is 65507 bytes). At least when you receive the packet, you'll know you have all the data. Use 65535 as the buffer size to ensure large packets can be fully received.

On sending, make sure you don't cross the size limits:

MAX_UDP_SIZE = 65507  # https://en.wikipedia.org/wiki/User_Datagram_Protocola = pickle.dumps(p)
if len(a) > MAX_UDP_SIZE:raise ValueError('Message too large')
s.sendto(a, (host, port))

and on the other side, use pickle.loads() to turn the pickle data stream back into an object:

UDP_MAX = 2 ** 16 - 1data, addr = s.recvfrom(UDP_MAX)
object = pickle.loads(data)

I urge you strongly to at the very least verify that addr is trusted, or you leave yourself open to executing arbitrary code. 65507 bytes is ample space to send a pickle that takes control of your process.

If you need to send more data, then you'll need to use TCP instead of UDP, because you'll have to send across data in a specific order, spread across multiple packets, and need for all packets to arrive on the receiving end; TCP provides that layer of reliability. At that point you'd have to prefix your pickle with a fixed number of bytes encoding the size of the pickle, so you can ensure you read that same amount of data again on the other side.

https://en.xdnf.cn/q/119300.html

Related Q&A

Google App Engine: Modifying 1000 entities

I have about 1000 user account entities like this:class UserAccount(ndb.Model):email = ndb.StringProperty()Some of these email values contain uppercase letters like [email protected]. I want to select …

more efficient method of dealing with large numbers in Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

MLM downline distribution count

I make my first MLM software and I think I managed to code how to get the points from the downline even though it is a recursive problem I didnt use recursion and I might refactor to a recursive versio…

Can someone please explain to me the purpose of the asterisk in Python? [duplicate]

This question already has answers here:What does asterisk * mean in Python? [duplicate](5 answers)How are pythons unpacking operators * and ** used?(1 answer)Closed 5 years ago.For instance, can some…

Linear Programming with cvxpy

I would like to ask you regarding on the Linear Program for optimization.I have an objective function, and constraint functions as below,variables(x1, x2, x3, x4, x5, x6) are quantities of the products…

Program runs forever without giving an error when plotting data only on continent [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

KeyError while perfoming solve of two equation

1st i need to get two equation of two longest line length i put lenghths with eq in list like these [( length 1 , eq 1 ) ,.....] sort list with reverse get two equation of two longest line when run the…

Most Pythonic way to merge two dictionnaries having common key/value pair

I have two lists of python dictionnaries : l1 = [{"id":1, "name":"A"}, {"id":2, "name":"B"}] l2 = [{"id":1, "full_name":&…

How to close a while True loop instantly Python

I have a problem ... How can i press P on my keyboard and close the entire program faster ( i would like instantly ) ? The script that i made runs in a loop ( Loop B ) and checks for an image on deskt…

How to replace a value in a list

the program asks user to enter 5 unique number, if the number is already in the list, ask for a new number. after 5 unique numbers have been entered, display the listnumbers = [1,2,3,4,5] count = 0 ind…