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)
相關文章
- Codeforces 245H Queries for Number of Palindromes:區間dp
- Format and un-format money/currency in JavaScriptORMJavaScript
- innobackupex: Error: --decompress requires qpressErrorUI
- Error format not a string literal and no format arguments解決方案ErrorORM
- Mysql date_format 與 Oracle to_char(date,’format’)MySqlORMOracle
- 'format' 詳解ORM
- SUN format命令ORM
- set excel formatExcelORM
- Json formatJSONORM
- 求助:TypeError: unsupported format string passed to NoneType.__format__ErrorORMNone
- 【C++】requires關鍵字簡介C++UI
- iOS signing for "***" requires a development team.iOSUIdev
- YouCompleteMe unavailable : requires Vim 7.4.143AIUI
- oracle工具 awr formatOracleORM
- vim Google style formatGoORM
- MYSQLDUMP TABLE IN SQL FORMATMySqlORM
- [Ruby]format xml with RubyORMXML
- JavaScript Number()JavaScript
- 問題解決:TypeError: unsupported format string passed to NoneType.__format__ErrorORMNone
- Oracle System Change Number (SCN) Number 完全筆記Oracle筆記
- 加強型WM_CONCAT2函式(不受32767限制的CLOB型別)函式型別
- 【NUMBER】Oracle的NUMBER資料型別特點Oracle資料型別
- VMware: ESXi requires the Execute Disable/No Execute CPU feature to be enabledUI
- JavaScript Number 物件JavaScript物件
- Number.NaNNaN
- Number of BoomerangsOOM
- jQuery Validate的format()用法jQueryORM
- mysql DATE_FORMAT函式MySqlORM函式
- C# String.FormatC#ORM
- C# string Format示例C#ORM
- Microsoft Windows Bitmap File Format SummaryROSWindowsORM
- row header format (157)HeaderORM
- Format of Index Blocks (207)ORMIndexBloC
- flutter doctor 時 提示 Flutter requires Android SDK 28 and ...【flutter】FlutterUIAndroid
- IE報vuex requires a Promise polyfill in this browser問題解決VueUIPromise
- react input[type='number']React
- JavaScript Number toLocaleString()JavaScript
- JavaScript Number toString()JavaScript