websoket/p1.py
developertrinidad08 7e6cf29479 first commit
2023-01-16 18:11:14 -03:00

27 lines
901 B
Python

import asyncio
import aiohttp
from aiohttp import web
from aiohttp.web_ws import WebSocketResponse
from pydub import AudioSegment
import os
import wave
async def handler(request):
counter = 0
ws = WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == aiohttp.WSMsgType.BINARY:
audio_data = msg.data
# wave
with wave.open("audio.wav", "wb") as audio_file:
audio_file.setsampwidth(2) #Sets the sample width of audio
audio_file.setnchannels(1) #Sets the number of audio channels
audio_file.setframerate(44100) #Sets the frame rate of audio
audio_file.writeframes(audio_data) #Writes audio data to file
return ws
app = web.Application()
app.add_routes([web.get('/', handler)])
web.run_app(app, host='localhost', port=8000)