Find max UDP payload -- python socket send/sendto
<p>How can I find the maximum length of a UDP payload in Python (Python 2), preferably platform-independent?</p>

<p>Specifically, I want to avoid <code>[Errno 90] Message too long</code> AKA <code>errno.EMSGSIZE</code>.</p>

<h3>Background</h3>

<ul>
<li>The maximum allowed by the UDP packet format <a href="http://stackoverflow.com/a/9853152/461834">is 65536</a>).</li>
<li><p>The maximum allowed by the IPv4 packet format <a href="http://stackoverflow.com/a/1098940/461834">seems to be 65507</a>.</p></li>
<li><p>Yet it seems that <a href="http://stackoverflow.com/a/35335138/461834">some systems set the limit lower</a>.</p></li>
</ul>

<h3>What I am not asking</h3>

<ul>
<li><a href="http://stackoverflow.com/questions/1098897/what-is-the-largest-safe-udp-packet-size-on-the-internet">What is the largest Safe UDP Packet Size on the Internet</a>?</li>
<li>What is the maximum UDP payload that can fit in one IPv4 datagram?</li>
<li>What is the maximum UDP payload that can fit in one Ethernet frame?</li>
</ul>

<h3>Demo Code</h3>

<p>To see the error in action:</p>

<pre><code>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))
</code></pre>

<p>Some of your premises need a minor correction. </p>

<p>As a result of the IP header containing a 16 bit field for length, the largest size an IPv4 message can be is <code>65535</code> bytes  And that includes the IP header itself.</p>

<p>The IP packet itself has at least a 20 byte header.  Hence 65535 - 20 == <code>65515</code> is the largest size of the <strong>payload</strong> of an IP message.  The payload can be a UDP datagram.</p>

<p>The UDP datagram itself is typically an 8 byte header.  Hence 65515 - 8 == <code>65507</code>.   So even if the UDP header could theoretically contain an amount higher than 65507 in its own length field, the IPv4 message can't contain it.</p>

<p>But if your system adds any more headers to the IP header (option fields via socket ioctls or whatever), then the limit of the UDP application payload will get reduced by a corresponding amount.</p>

<p>In practice, any IP message above the MTU size of your network adapter (~1500 bytes), will trigger the UDP packet to undergo IP fragmentation.  So if your ethernet card has a 1500 byte message size, a a UDP datagram containing 65507 bytes of application data will get fragmented into approximately 43 separate ethernet frames.  Each frame is a fragmented IP packet containing a subset of the UDP bytes, but with a seperate header.  When all the IP fragments are received on the remote end, it logically gets delivered to the application as 65507 byte datagram.  Fragmentation is transparent to applications.</p>

<p>I would suggest running your code with Wireshark and send to a real IP address out of the network.  You can observe and study how IP fragmentation works.</p>

python sockets networking udp