09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 2 ■ UDP<br />

give us good practice with the <strong>Python</strong> network API before we move on to the additional complications<br />

that are brought by the use <strong>of</strong> TCP.<br />

Should You Read This Chapter?<br />

Yes, you should read this chapter—and the next one on TCP—if you are going to be doing any<br />

programming on an IP network. The issues raised and answered are simply too fundamental. A good<br />

understanding <strong>of</strong> what is happening down at these low levels will serve you very well, regardless <strong>of</strong><br />

whether you are fetching pages from a web server, or sending complicated queries to an industrial<br />

database.<br />

But should you use what you learn in this chapter? Probably not! Unless you are talking to a service<br />

that already speaks UDP because <strong>of</strong> someone else’s decision, you will probably want to use something<br />

else. The days when it was useful to sit down with a UDP connection and bang out packets toward<br />

another machine are very nearly gone.<br />

The deployment <strong>of</strong> UDP is even rather dangerous for the general health <strong>of</strong> the IP network. The<br />

sophisticated TCP protocol will automatically back <strong>of</strong>f as the network becomes saturated and starts to<br />

drop packets. But few UDP programmers want to even think about the complexity <strong>of</strong> typical congestionavoidance<br />

algorithms—much less implement them correctly—with the result that a naively-written<br />

application atop UDP can bring a network to its knees, flooding your bandwidth with an increasing<br />

number <strong>of</strong> re-tries until almost no requests are actually getting through successfully.<br />

If you even think you want to use the UDP protocol, then you probably want to use a message queue<br />

system instead. Take a look at Chapter 8, and you will probably find that ØMQ lets you do everything you<br />

wanted to accomplish with UDP, while having been programmed by people who dove far deeper into<br />

the efficiencies and quirks <strong>of</strong> the typical operating system network stack than you could do without<br />

months <strong>of</strong> research. If you need persistence or a broker, then try one <strong>of</strong> the message queues that come<br />

with their own servers for moving messages between parts <strong>of</strong> your application.<br />

Use UDP only if you really want to be interacting with a very low level <strong>of</strong> the IP network stack. But,<br />

again, be sure to read this whole chapter either way, so that you know the details <strong>of</strong> what lies beneath<br />

some <strong>of</strong> your favorite protocols like DNS, real-time audio and video chat, and DHCP.<br />

Addresses and Port Numbers<br />

The IP protocol that we learned about in Chapter 1 assigns an IP address—which traditionally takes the<br />

form <strong>of</strong> a four-octet code, like 18.9.22.69—to every machine connected to an IP network. In fact, it does<br />

a bit more than this: a machine with several network cards connected to the network will typically have a<br />

different IP address for each card, so that other hosts can choose the network over which you want to<br />

contact the machine. Multiple interfaces are also used to improve redundancy and bandwidth.<br />

But even if an IP-connected machine has only one network card, we learned that it also has at least<br />

one other network address: the address 127.0.0.1 is how machines can connect to themselves. It serves<br />

as a stable name that each machine has for itself, that stays the same as network cables are plugged and<br />

unplugged and as wireless signals come and go.<br />

And these IP addresses allow millions <strong>of</strong> different machines, using all sorts <strong>of</strong> different network<br />

hardware, to pass packets to each other over the fabric <strong>of</strong> an IP network.<br />

But with UDP and TCP we now take a big step, and stop thinking about the routing needs <strong>of</strong> the<br />

network as a whole and start considering the needs <strong>of</strong> specific applications that are running on a<br />

particular machine. And the first thing we notice is that a single computer today can have many dozens<br />

<strong>of</strong> programs running on it at any given time—and many <strong>of</strong> these will want to use the network at the same<br />

moment! You might be checking e-mail with Thunderbird while a web page is downloading in Google<br />

Chrome, or installing a <strong>Python</strong> package with pip over the network while checking the status <strong>of</strong> a remote<br />

16

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!