Loading video player…

Programming Sockets in Python (Overview)

Sockets and the socket API are used to send messages across a network. They provide a form of inter-process communication (IPC). The network can be a logical, local network to the computer, or one that’s physically connected to an external network, with its own connections to other networks. The obvious example is the Internet, which you connect to via your ISP.

In this video course, you’ll create:

  • A simple socket server and client
  • An improved version that handles multiple connections simultaneously
  • A server-client application that functions like a full-fledged socket application, complete with its own custom header and content
Download

Sample Code (.zip)

12.3 KB
Download

Sample Code (.zip)

12.3 KB

00:00 Welcome to Programming Sockets in Python. My name is Christopher, and I will be your guide. This course is about socket programming. A socket is what you use to interact with networks from your code.

00:12 The Python socket module is a low-level library that is a thin wrapper for the underlying C code. That means it isn’t your friendliest neighborhood and you have to write code to manage more things than your typical Python program.

00:25 But this course will show you the ropes. In this course, you’ll write a couple of servers that listen to sockets and manage connections, as well as write several clients to talk to those same servers.

00:38 At the base socket level, you’ll be slinging bytes that gets tiring after a while, so you’ll probably want a higher-level abstraction. That’s where application protocols come in.

00:47 You’ll learn how to consume those raw bytes and turn them into useful Python content.

00:54 The code in this course was tested with Python 3.13. The socket library has been around forever, so pretty much any Python 3 should work. I also use several network tools along the way, which if you want to duplicate everything I’m doing, you’ll have to install.

01:09 I’ll point them out as I go along.

01:14 One of the challenges when writing an intermediate or advanced course is knowing your audience. For a beginner course, you have to start at the beginning.

01:21 This course isn’t that. Since I’m not sure how much networking knowledge you have, I’ve provided some background. If you’re comfortable with network stuff already, you might want to skip forwards.

01:32 If you’re already familiar with IP addresses, ports, network interfaces, and understand how a protocol like HTTP sits on top of TCP/IP and all you want is to see sockets in Python, you could skip to the Using Sockets section, the first lesson of which is titled Listening on a Socket.

01:51 If you’re okay with IP addresses and ports, but could do with a refresher on what a packet looks like, then maybe skip forward to seeing TCP/IP. Otherwise, I’d suggest just continuing from this lesson onto the next one.

02:07 Speaking of the next one, next up, what happens on the network when you fetch a web page?

Avatar image for Jordan Bell

Jordan Bell on Dec. 17, 2024

On the question of how much preliminaries to present for an intermediate course… What can be helpful is highlighting/emphasizing certain pieces of background, but not feeling obliged to go through the 7 layer OSI model and the 4 layer TCP model, but maybe to say a few penetrating words that people who’ve seen those multiple times might find illuminating.

Become a Member to join the conversation.