TypeError: 'bytearray' object cannot be interpreted as an integer
TypeError: 'bytearray' object cannot be interpreted as an integer
我想通过 HTTP 发送音频数据,但我不明白为什么会出现此异常:
Exception happened during processing of request from ('127.0.0.1', 59976)
Traceback (most recent call last):
File "/usr/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/usr/lib/python3.6/http/server.py", line 418, in handle
self.handle_one_request()
File "/usr/lib/python3.6/http/server.py", line 406, in handle_one_request
method()
File "/home/vivanov/temp.py", line 113, in do_GET
data.append(bytearray(stream.read(CHUNK)))
TypeError: 'bytearray' object cannot be interpreted as an integer
问题似乎与将值传递给 wfile.write
有关。
我该如何解决这个问题?
这是我的代码:
class ChunkingRequestHandler(BaseHTTPRequestHandler):
ALWAYS_SEND_SOME = False
ALLOW_GZIP = False
protocol_version = 'HTTP/1.1'
def do_GET(self):
ae = self.headers.get('accept-encoding') or ''
# send some headers
self.send_response(200)
self.send_header('Transfer-Encoding', 'chunked')
self.send_header('Content-type', 'audio/x-wav')
self.end_headers()
data = bytearray(wav_header)
data.append(bytearray(stream.read(CHUNK)))
print(data)
self.wfile.write(b"%X\r\n%s\r\n" % (len(data), data))
while True:
data = bytearray(stream.read(CHUNK))
self.wfile.write(b"%X\r\n%s\r\n" % (len(data), data))
# send the chunked trailer
self.wfile.write('0\r\n\r\n')
尽管名称如此,但如果您想一次将多个元素附加到 Python 中的类列表对象,则不能使用 append
方法。 bytearray
就像一个字节列表,因此将另一个 bytearray
附加或连接到它的方法是使用 extend
方法,或 +=
:
data += bytearray(...)
# OR
data.extend(bytearray(...))
事实上,如果您要添加到 bytearray
的内容已经可以传递给 bytearray()
构造函数,则您可能不需要将其包装在 bytearray()
。例如bytes
个对象(如b'something'
)可以直接添加:
data += b'something'
一旦你修复了那行代码,你可能会在其他行上遇到问题。例如,如果 wfile.write
采用 bytes
,那么向它发送一个像 '0\r\n\r\n'
这样的 unicode 字符串可能会出错;看起来你打算写 b'0\r\n\r\n'
.
实际上这是我最终工作的最终代码,可能对某人有用:
class ChunkingRequestHandler(BaseHTTPRequestHandler):
ALWAYS_SEND_SOME = False
ALLOW_GZIP = False
protocol_version = 'HTTP/1.1'
def do_GET(self):
ae = self.headers.get('accept-encoding') or ''
# send some headers
self.send_response(200)
self.send_header('Content-type', 'audio/x-wav')
self.end_headers()
data = []
for i in wav_header:
data.append(int(i))
for i in stream.read(CHUNK):
data.append(int(i))
self.wfile.write(bytearray(data))
while True:
data = []
for i in stream.read(CHUNK):
data.append(int(i))
self.wfile.write(bytearray(data))
# send the chunked trailer
self.wfile.write('0\r\n\r\n')
我想通过 HTTP 发送音频数据,但我不明白为什么会出现此异常:
Exception happened during processing of request from ('127.0.0.1', 59976) Traceback (most recent call last): File "/usr/lib/python3.6/socketserver.py", line 654, in process_request_thread self.finish_request(request, client_address) File "/usr/lib/python3.6/socketserver.py", line 364, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/lib/python3.6/socketserver.py", line 724, in __init__ self.handle() File "/usr/lib/python3.6/http/server.py", line 418, in handle self.handle_one_request() File "/usr/lib/python3.6/http/server.py", line 406, in handle_one_request method() File "/home/vivanov/temp.py", line 113, in do_GET data.append(bytearray(stream.read(CHUNK))) TypeError: 'bytearray' object cannot be interpreted as an integer
问题似乎与将值传递给 wfile.write
有关。
我该如何解决这个问题?
这是我的代码:
class ChunkingRequestHandler(BaseHTTPRequestHandler):
ALWAYS_SEND_SOME = False
ALLOW_GZIP = False
protocol_version = 'HTTP/1.1'
def do_GET(self):
ae = self.headers.get('accept-encoding') or ''
# send some headers
self.send_response(200)
self.send_header('Transfer-Encoding', 'chunked')
self.send_header('Content-type', 'audio/x-wav')
self.end_headers()
data = bytearray(wav_header)
data.append(bytearray(stream.read(CHUNK)))
print(data)
self.wfile.write(b"%X\r\n%s\r\n" % (len(data), data))
while True:
data = bytearray(stream.read(CHUNK))
self.wfile.write(b"%X\r\n%s\r\n" % (len(data), data))
# send the chunked trailer
self.wfile.write('0\r\n\r\n')
尽管名称如此,但如果您想一次将多个元素附加到 Python 中的类列表对象,则不能使用 append
方法。 bytearray
就像一个字节列表,因此将另一个 bytearray
附加或连接到它的方法是使用 extend
方法,或 +=
:
data += bytearray(...)
# OR
data.extend(bytearray(...))
事实上,如果您要添加到 bytearray
的内容已经可以传递给 bytearray()
构造函数,则您可能不需要将其包装在 bytearray()
。例如bytes
个对象(如b'something'
)可以直接添加:
data += b'something'
一旦你修复了那行代码,你可能会在其他行上遇到问题。例如,如果 wfile.write
采用 bytes
,那么向它发送一个像 '0\r\n\r\n'
这样的 unicode 字符串可能会出错;看起来你打算写 b'0\r\n\r\n'
.
实际上这是我最终工作的最终代码,可能对某人有用:
class ChunkingRequestHandler(BaseHTTPRequestHandler):
ALWAYS_SEND_SOME = False
ALLOW_GZIP = False
protocol_version = 'HTTP/1.1'
def do_GET(self):
ae = self.headers.get('accept-encoding') or ''
# send some headers
self.send_response(200)
self.send_header('Content-type', 'audio/x-wav')
self.end_headers()
data = []
for i in wav_header:
data.append(int(i))
for i in stream.read(CHUNK):
data.append(int(i))
self.wfile.write(bytearray(data))
while True:
data = []
for i in stream.read(CHUNK):
data.append(int(i))
self.wfile.write(bytearray(data))
# send the chunked trailer
self.wfile.write('0\r\n\r\n')