Python’s New Out-of-Range Port Number Handling

You heard that right, no longer will you have to worry about those ***** “invalid port numbers” errors when trying to connect to a server on an unconventional port.

Before we dive into this groundbreaking development, let’s take a quick trip down memory lane and reminisce about the good old days of Python 3.8 (or earlier). Remember how frustrating it was to try and connect to a server using a non-standard port number? You would get an error message that looked something like this:

# Import the socket module
import socket

# Create a socket object using the AF_INET address family and the SOCK_STREAM socket type
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to the server at the specified host and port number
s.connect(('example.com', 9001)) # non-standard port number

# If the connection is refused, an error message will be displayed
# This is because the server is not listening on the specified port number
# The error message will indicate the specific error, in this case, ConnectionRefusedError
# The error number (Errno 111) can also provide more information about the error
# This error can occur if the server is not running or if the port number is incorrect

Ugh! What’s the deal with this “invalid port number” error? Well, it turns out that Python was being a stickler for the rules. According to RFC 793 (the Transmission Control Protocol), valid TCP and UDP ports range from 0 to 65535. Any value outside of this range is considered invalid.

Chill out, don’t worry, dear programmer! In Python 3.9, we’ve introduced a new feature that allows you to connect to servers on any port number even if it’s not in the valid range. This means you can now use those obscure ports for your own nefarious purposes without having to worry about ***** errors getting in your way!

So how does this magical new feature work? Well, let me explain. In Python 3.9 (and later), when you try to connect to a server on an invalid port number, instead of raising a ConnectionRefusedError or some other error message, the socket module will simply return None. That’s right no more errors!

Here’s what it looks like in code:

# Import the socket module
import socket

# Create a socket object using the AF_INET address family and SOCK_STREAM socket type
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to the server at example.com on port 9001
# Note: This is a non-standard port number
s.connect(('example.com', 9001))

# The socket module will return None instead of raising an error if the connection fails
# This is a new feature in Python 3.9 and later versions
# This means that there will be no error message if the connection is refused or invalid
# Instead, the script will simply return None
# This can be useful for handling errors in a more controlled manner
None

That’s right no more errors! You can now use any port number you want without having to worry about ***** exceptions getting in your way. This is a huge improvement over previous versions of Python, where trying to connect to an invalid port would result in a ConnectionRefusedError or some other error message that could be confusing and difficult to debug.

It’s one of the most exciting new features in Python 3.9 (or later), and we can’t wait for you to try it out!

SICORPS