struct.error: 'h' format requires -32768 number 32767
參考: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)
相關文章
- Format and un-format money/currency in JavaScriptORMJavaScript
- Json formatJSONORM
- 求助:TypeError: unsupported format string passed to NoneType.__format__ErrorORMNone
- set excel formatExcelORM
- bug solved | This experimental syntax requires enabling xxxUI
- 問題解決:TypeError: unsupported format string passed to NoneType.__format__ErrorORMNone
- vim Google style formatGoORM
- [Ruby]format xml with RubyORMXML
- oracle工具 awr formatOracleORM
- 【C++】requires關鍵字簡介C++UI
- JavaScript Number()JavaScript
- jQuery Validate的format()用法jQueryORM
- 8.4.2. bytea Escape FormatORM
- Kata:Hamming number
- JavaScript Number toLocaleString()JavaScript
- JavaScript Number toString()JavaScript
- Number.NaNNaN
- JavaScript Number 物件JavaScript物件
- Leetcode Number of islandsLeetCode
- 事務傳播機制之REQUIRES_NEWUI
- flutter doctor 時 提示 Flutter requires Android SDK 28 and ...【flutter】FlutterUIAndroid
- JAVA中String format的用法JavaORM
- [#181024][PAT Practice] A+B FormatORM
- 針對python錯誤 format()PythonORM
- Requires: libc.so.6(GLIBC_2.14)(64bit)UI
- Perfect Number 完美數
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- Leetcode 9 Palindrome NumberLeetCode
- Number.parseInt() 方法
- Number.parseFloat()方法
- Number.isSafeInteger()方法
- Number.isNaN()方法NaN
- Number.isFinite()方法
- Js中的NumberJS
- Number.POSITIVE_INFINITY
- Number.ATIVE_INFINITY
- Big Number hd 1212