我正在尝试通过python发送mp3文件。以下是代码 -
for v in videos:
Thread(target=video_download, args=(handler,v,videos)).start()
def video_download(handler,v,videos):
try:
ydl_opts['format'] = 'mp4'
ydl(ydl_opts).download([v])
except:
ydl_opts['format'] = 'best'
ydl(ydl_opts).download([v])
mp3 = open('./'+v+'.mp3', "r+").read()
handler.send_header('Content-Disposition', 'attachment; filename=%s.mp3' % videos[v])
handler.wfile.write(mp3)
handler.end_headers()
它从youtube下载m4a格式的视频并将其转换为mp3文件。下载和转换部分工作正常,它使用线程,但使用wfile.write()发送文件时,我收到以下错误
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "/Users/Keshav/Documents/Github/Webserver/webserver/Downld.py", line 37, in video_download
handler.send_header('Content-Disposition', 'attachment; filename=%s.mp3' % videos[v])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 401, in send_header
self.wfile.write("%s: %s\r\n" % (keyword, value))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 324, in write
self.flush()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
AttributeError: 'NoneType' object has no attribute 'sendall'
如果我实现多处理而不是线程,那么同样的工作完全正常。但它是连续处理而不是并行处理。