How to bind a Python socket to a specific domain?
<p>I have a Heroku application that has a domain <code>moarcatz.tk</code>. It listens for non-HTTP requests using Python's <code>socket</code>.<br>
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 <code>moarcatz.tk</code> would fix the problem. But I don't know how to bind a socket to a domain name.<br>
I tried <code>'moarcatz.tk</code>' and <code>gethostbyname('moarcatz.tk')</code>, but both give me this error:</p>

<pre><code>OSError: [Errno 99] Cannot assign requested address
</code></pre>

<p>What's up with that?</p>

<p>You can't control this via your code, but you can control this via Heroku.</p>

<p>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.</p>

<p>What you do is this:</p>

<pre><code>heroku domains:add www.moarcatz.tk
</code></pre>

<p>Then, go to your DNS provider for <code>moarcatz.tk</code> and add a CNAME record for:</p>

<pre><code>www &lt;heroku-app-name&gt;.herokuapp.com
</code></pre>

<p>This will do two things:</p>

<ul>
<li>Point your DNS to Heroku.</li>
<li>Make Heroku filter the incoming traffic and ALLOW it for that specific domain.</li>
</ul>

python sockets heroku