场景是这个样子的,我这边要实现一个接口, 服务器端是java做的,客户端是python做的,服务器端向客户端提供了一个login的接口,需要客户端实现,login需要给服务器返回一个byte[] 的值 ,但是python中貌似没有byte这个类型,我该怎么处理?
bytearray 这个方法试过了 貌似不行 在线等好心人 求大神给我点一下啊 卡到这里很长时间了,很紧 啊
https://gist.github.com/igniteflow/1237391
正确答案 已经试过 成功 感谢开源社区 感谢 华为 刘峥
import base64
"""
Some useful functions for interacting with Java web services from Python.
"""
def make_file_java_byte_array_compatible(file_obj):
"""
Reads in a file and converts it to a format accepted as Java byte array
:param file object
:return string
"""
encoded_data = base64.b64encode(file_obj.read())
print encoded_data
strg = ''
for i in xrange((len(encoded_data)/40)+1):
strg += encoded_data[i*40:(i+1)*40]
return strg
def java_byte_array_to_binary(file_obj):
"""
Converts a java byte array to a binary stream
:param java byte array as string (pass in as a file like object, can use StringIO)
:return binary string
"""
decoded_data = base64.b64decode(file_obj.read())
strg = ''
for i in xrange((len(decoded_data)/40)+1):
strg += decoded_data[i*40:(i+1)*40]
return strg
bytearray是将字符串在内存中的存储转化为数组,而不是它的字面值。
比如 "1A" 转化出来是2个字节,而不是字面值的一个字节。
python无需定义类型,直接接收就可以了。
关于python字节流,参考:http://blog.sina.com.cn/s/blog_4b5039210100f1tu.html
lz值得赞一个,不但解决了问题,而且分享了解决方案。
python的这种技术也不怎么懂
这里是一些资料 希望对楼主有用啊
http://download.csdn.net/album/detail/1239