@for ever 2012-06-25
在python32版本下面,下面的代码:
msg = pack(">H%ds" % len(reason), code, reason)
struct.error: argument for 's' must be a bytes object
msg = pack(">H%ds" % len(reason), code, reason.encode('utf-8'))
关于struct.pack函数,参数个数是无限的。第一个参数定义打包格式,
剩余的所有参数都是要打包的内容。
第一个格式参数具体写法如下:
Format c Type Python Note
x pad byte no value
c char string of length 1
b signedchar integer
B unsignedchar integer
? _Bool bool (1)
h short integer
H unsignedshort integer
i int integer
I unsignedint integer or long
l long integer
L unsignedlong long
q longlong long (2)
Q unsignedlonglong long (2)
f float float
d double float
s char[] string
p char[] string
P void* long
此外,还包含相应的大/小端设置(如果忽略该设置,默认<):
@ native native
= native standard
< little-endian standard
> big-endian standard
! network (= big-endian) standard
@forandever 2012-6-25