开发者

posting pickle dumps string with urllib

开发者 https://www.devze.com 2023-04-07 03:38 出处:网络
I need to post data to a Django server. I\'d like to use pickle. (There\'re no security requirements -> small intranet app.)

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(sel开发者_开发知识库f.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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号