在两个Django服务器之间传递pickle文件或Python对象

时间:2017-11-29 13:48:02

标签: python django httpresponse pickle python-object

我正在构建一个小应用程序,需要两个服务来交换pickle文件或python对象,任何人都可以。由于我是Django的新手,我很难这样做。请看代码。 发信人:

def get(self, request):
    message ="hello"
    pickle.dump(message, open("test.p", "wb"))
    filename = "test.p"
    #content = 'any string generated by django'
    response = HttpResponse(message, mime_type='application/python-pickle')
    response['Content-Disposition'] = 'attachment; filename={0}'.format(filename)
    return response

接收器:

with open("sample1.p", 'wb') as fd:
    for chunk in response.iter_content():
        fd.write(chunk)

with open("sample1.p", 'rb') as handle:
    var = pickle.load(handle)

以上代码无效。它生成空文件或KeyError 101

0 个答案:

没有答案
相关问题