How can I incorporate VOIP using python?

Viewed 128

I am currently trying to use Socket to multicast audio in real time to an IP and Port.

import socket

MCAST_GRP = '000.0.0.00'
MCAST_PORT = 00000

MULTICAST_TTL = 2

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, MULTICAST_TTL)

with open('C:\\Users\\User\\Downloads\\dog_bark_x.wav', 'rb') as f:

  for l in f:  
    sock.sendto(sock.sendall(l), (MCAST_GRP, MCAST_PORT))
sock.close()

I am currently testing this by using a WAV file. however when i run the code I receive this error:

[WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

I can send strings without error using code below, meaning the client is connected and listening so im not sure why i am encountering the error above:

import socket

MCAST_GRP = '000.0.0.00'
MCAST_PORT = 00000

MULTICAST_TTL = 2

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, MULTICAST_TTL)

sock.sendto("Test".encode(), (MCAST_GRP, MCAST_PORT))

1 Answers
sock.sendto(sock.sendall(l), (MCAST_GRP, MCAST_PORT))

The inner sock.sendall(l) is working on the unconnected socket, that's why the error. It is likely that you did not mean to use sendall here at all but simply

sock.sendto(l, (MCAST_GRP, MCAST_PORT))

Note though that you are using UDP here which is an unreliable protocol, i.e. datagrams might be lost, duplicated or reordered during transmit. You thus cannot expect that the data will be read by the recipient exactly as they were sent.

Apart from that it is not a good idea to use for line in file when reading binary data.

Related