在Python中从POST请求读取二进制数据

2024-09-26 22:51:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我用POST从java发送二进制数据。以下是java代码:

URL url = new URL(saveURI);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setInstanceFollowRedirects(false);
urlConnection.setUseCaches(false);

urlConnection.setRequestProperty("Content-Type", "application/octet-stream");
urlConnection.setRequestProperty("charset", "utf-8");
urlConnection.setRequestProperty("Content-Length", Integer.toString(length));
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(data);
out.flush();
out.close();

大小为150216字节。python代码:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys

print "Content-Type: text/plain;charset=utf-8"
print

data = sys.stdin.read()
print len(data)

profile= profile_pb2.ProtoCharacter()
profile.ParseFromString(data)
if profile.IsInitialized():
    ProfileWork.saveProfile(profile)
    print profile.tag

给我22分。有什么问题吗?你知道吗

编辑:添加了更多代码


Tags: 代码trueurlnewdatajavacontentout

热门问题