Sockets are the lead pipes of computer networks: they let you connect with other devices so that information can flow freely. As you might expect, they're widely used on the Internet. Peyton McCullough explains how to code sockets in Python.
One of the must-know features of any language is sockets, and, no, Iím not talking about the ones positioned on your ceiling. While many novice scripters will run away at the sound of the word, sockets in Python are not hard to learn and work with at all. In this article, I will explain the basics and even apply sockets in a real-world situation.
Please note that this article assumes that you have a basic command of the Python language. If not, you can either read the official tutorial or buy a book Ė or both. If youíre looking for a good book, you should head over to OíReilly & Associates.
With that stated, letís get started.
The Anatomy of a Socket
Letís start off simple: what is a socket? The easiest way to learn is to compare computer sockets to lead pipes. Letís say you have some sort of machine that has two pipes. One pipe pumps in water and the other pipe pumps out water. This will be your server. Now, imagine you have a simple box that has one pipe going through it. Letís say that you hook it up to your pump machine. It will receive the water from the machine and then give water back. This will be your client. If you wanted to, you could hook up more machines and pipes to this to form a network of clients and servers.
Sockets are useful. The Web browser that you are viewing this page with is connecting to the server that this page is in by way of a socket. If you were to connect to one of your favorite online games or chat networks, you would be using sockets as well. As you can see, sockets are both both powerful and widely-used.