struct.error: 'h' format requires -32768 number 32767

ShellCollector發表於2019-03-03

參考:https://blog.csdn.net/weiwangchao_/article/details/80395941

struct.error: 'h' format requires -32768 <= number <= 32767

換成了"H",H長度是0-65536,傳送圖片暫時夠用。

程式碼如下:

#傳送websocket server報文部分
def sendMessage(self, message):
    msgLen = len(message)
    backMsgList = []
    backMsgList.append(struct.pack('B', 129+1))

    if msgLen <= 125:
        backMsgList.append(struct.pack('b', msgLen))
    elif msgLen <=65535:
        backMsgList.append(struct.pack('b', 126))
        backMsgList.append(struct.pack('>H', msgLen))
    elif msgLen <= (2^64-1):
        backMsgList.append(struct.pack('b', 127))
        backMsgList.append(struct.pack('>h', msgLen))
    else :
        print("the message is too long to send in a time")
        return
    message_byte = bytes()
    print(type(backMsgList[0]))
    for c in backMsgList:
        # if type(c) != bytes:
        # print(bytes(c, encoding="utf8"))
        message_byte += c
    message_byte += bytes(message, encoding="utf8")
    #print("message_str : ", str(message_byte))
    # print("message_byte : ", bytes(message_str, encoding="utf8"))
    # print(message_str[0], message_str[4:])
    # self.connection.send(bytes("0x810x010x63", encoding="utf8"))
    self.connection.send(message_byte)
 

解決方法:

def send_img(msg):
    token = b'\x82'
    length = len(msg)
    if length <= 125:
        token += struct.pack('B', length)
    elif length <= 0xFFFF:
        token += struct.pack('!BH', 126, length)
    else:
        token += struct.pack('!BQ', 127, length)
    message_byte = token + msg
    global conn_dict_face
    for connection in conn_dict_face.values():
        if message_byte != None:
            try:
                #if not connection._closed:
                timestr = datetime.datetime.now().strftime('%H:%M:%S:%f')
                connection.send(message_byte)
            except Exception as e:
                log_info('send error', e)
                connection.close()
        else:
            log_info("msg_test is none")

 

1. struct模組
struct.pack(fmt, value1, value2)
fmt為由特定字元組成的字串,函式功能為,將python資料型別value1,value2轉化為C資料型別

fmt字元型別:

Format  C Type          Python type     Standard size
x       pad byte        no value         
c       char            bytes of length 1 
b       signed char     integer         1   
B       unsigned char   integer         1   
?       _Bool           bool            1   
h       short           integer         2   
H       unsigned short  integer         2   
i       int             integer         4   
I       unsigned int    integer         4   
l       long            integer         4   
L       unsigned long   integer         4   
q       long long       integer         8   
Q       unsigned long long  integer     8   
n       ssize_t         integer             
N       size_t          integer             
e       (7)             float           2   
f       float           float           4   
d       double          float           8   
s       char[]          bytes            
p       char[]          bytes            
P       void *          integer         (6)

struct.unpack(fmt, buffer)
 

相關文章