Knack
Knack

Reputation: 1134

posting pickle dumps string with urllib

I need to post data to a Django server. I'd like to use pickle. (There're no security requirements -> small intranet app.)

First, I pickle the data on the client and sending it with urllib2

def dispatch(self, func_name, *args, **kwargs):
    dispatch_url = urljoin(self.base_url, "api/dispatch")
    pickled_args = cPickle.dumps(args, 2)
    pickled_kwargs = cPickle.dumps(kwargs, 2)
    data = urllib.urlencode({'func_name' : func_name,
                             'args' : pickled_args,
                             'kwargs': pickled_kwargs})
    resp = self.opener.open(dispatch_url, data)

Recieving the data at the server works, too:

def dispatch(request):
    func_name = request.POST["func_name"]
    pickled_args = request.POST["args"]
    pickled_kwargs = request.POST["kwargs"]

But unpickling raises an error:

cPickle.loads(pickled_args)

Traceback (most recent call last):
File "<string>", line 1, in <fragment>
TypeError: must be string, not unicode

Obviously the urllib.urlencode has created a unicode string. But how can I convert it back to be able to unpickling (laods) again?

By the way, using pickle format 0 (ascii) works. I can convert to string before unpickling, but I'd rather use format 2.

Also, recommendations about how to get binary data to a Django view are highly appreciated.

Upvotes: 0

Views: 1369

Answers (1)

jfs
jfs

Reputation: 414685

Obviously the urllib.urlencode has created a unicode string.

urllib.urlencode doesn't return Unicode string.
It might be a feature of the web framework you use that request.POST contains Unicode strings.

Don't use pickle to communicate between services it is not secure, brittle, not portable, hard to debug and it makes your components too coupled.

Upvotes: 2

Related Questions