Leva7 Leva7 - 6 months ago 57
Python Question

How to bind a Python socket to a specific domain?

I have a Heroku application that has a domain

. It listens for non-HTTP requests using Python's

The documenatation states that if I bind a socket to an empty string as an IP address, it will listen on all available interfaces. I kept getting empty requests from various IP addresses, so I assume that setting the socket to only listen for connections to
would fix the problem. But I don't know how to bind a socket to a domain name.

I tried
' and
, but both give me this error:

OSError: [Errno 99] Cannot assign requested address

What's up with that?


You can't control this via your code, but you can control this via Heroku.

Heroku has a pretty nifty DNS CNAME tool you can use to ensure your app ONLY listens to incoming requests for specific domains -- it's part of the core Heroku platform.

What you do is this:

heroku domains:add www.moarcatz.tk

Then, go to your DNS provider for moarcatz.tk and add a CNAME record for:

www <heroku-app-name>.herokuapp.com

This will do two things:

  • Point your DNS to Heroku.
  • Make Heroku filter the incoming traffic and ALLOW it for that specific domain.