How can I find the maximum length of a UDP payload in Python (Python 2), preferably platform-independent?
Specifically, I want to avoid [Errno 90] Message too long
AKA errno.EMSGSIZE
.
Background
- The maximum allowed by the UDP packet format is 65536).
The maximum allowed by the IPv4 packet format seems to be 65507.
Yet it seems that some systems set the limit lower.
What I am not asking
- What is the largest Safe UDP Packet Size on the Internet?
- What is the maximum UDP payload that can fit in one IPv4 datagram?
- What is the maximum UDP payload that can fit in one Ethernet frame?
Demo Code
To see the error in action:
import socket
msg_len = 65537 # Not even possible!
ip_address = "127.0.0.1"
port = 5005
msg = "A" * msg_len
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(msg, (ip_address, port))