Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Programming Sockets in Python (Summary)

You covered a lot of ground in this video course! Networking and sockets are large subjects. If you’re new to networking or sockets, don’t be discouraged by all of the terms and acronyms.

There are a lot of pieces to become familiar with in order to understand how everything works together. However, just like Python, it will start to make more sense as you get to know the individual pieces and spend more time with them.

In this video course, you:

  • Looked at the low-level socket API in Python’s socket module and saw how it can be used to create client-server applications
  • Built a client and server that can handle multiple connections using a selectors object
  • Created your own custom class and used it as an application-layer protocol to exchange messages and data between endpoints

From here, you can use your custom class and build upon it to learn and help make creating your own socket applications easier and faster.

Download

Sample Code (.zip)

12.3 KB
Download

Sample Code (.zip)

12.3 KB

00:00 In the previous lesson, I pointed you at some useful networking tools. This lesson summarizes the course and covers other sources of information you might be interested in.

00:10 The Python socket module can be used to write both network client and server programs. When you create a socket, you need to specify what network protocol you’ll be using.

00:19 Most of the time, this will be IPv4. When you’re writing a server socket, you create a socket instance, bind the socket to an address and port listen to the socket, accept a connection on the socket, then use that connection to send and receive data.

00:36 If you’re writing a client, there’s less work to do. You create a socket instance, then connect to the server, specifying the address and port, then send and receive data.

00:47 The most common network protocol is IPv4, which is the backbone of the internet. It uses an address port combination. Ports are 16-bit bytes with the first kilobyte of ports reserved for well-known protocols. On most computers, you need administrative access to be able to listen on any of these ports.

01:07 Ports are tied to specific services. For example, 80 and 443 get used for the web. You also get a port number on the source side, but this is automatically assigned for you when you connect to the server.

01:21 If you only listen to a socket, you’ll be blocking that socket, meaning your service can only talk with one client at a time. One way around this is to use a selector to multiplex I/O.

01:31 The I/O in this case would be the multiple sockets you’re allowing to connect. Each time someone connects, you register the new connection with the selector.

01:40 Then the selector calls your callback function, sending, read and write events telling you it’s time to read or write to the socket. Most programs don’t deal with raw bytes, but use a higher-level application protocol.

01:51 In this course, you saw how to create your own protocol and one possible structure for the data so you’d know how many bytes to take off the stream to correspond to a single application message.

02:03 The Python socket documentation is very comprehensive if a bit low level, so you might also want to check out the how -to guide that shows you socket coding recipes.

02:14 To learn more about bits and bytes, you could take this course,

02:18 or if my quickly mentioning string encoding left you with a bunch of questions, this course might have some of the answers. And finally, if you’d like to try concurrency instead of using a selector, this learning path has multiple courses and tutorials worth diving into.

02:34 That’s all from me. I hope you enjoyed the course. Bork, bork, bork.

Become a Member to join the conversation.