title: Find Host and Port in a Django Application
body: <p>I'm connecting to the Twitter Streaming API and am setting up the OAuth handshake.  I need to request a token and send a <code>callback_url</code> as a params dictionary along with post request.</p>
  
  <p>I've hardcoded in the url for development <code>(http://localhost:8000/oauth)</code> but when I deploy this will change.  I want to set up something that will find the host and port and set a reference it.  Ideally, looking like <code>"http://%s/oauth" % (domain_name)</code></p>
  
  <p>I've tried using both the os and socket modules, and code is below:</p>
  
  <pre><code>class OAuth:
  
      def request_oauthtoken(self):
          name = socket.gethostname()
          ip = socket.gethostbyname(name)
          domain = socket.gethostbyaddr(ip) # Sequence attempts to find the current domain name.  This will add expandability to the calling the API, as opposed to hard coding it in.  It's not returning what I'm expecting
  
  
          payload = { 'oauth_callback': 'http://localhost:8000/oauth' }
          print(domain)
          return payload
  </code></pre>
  
  <p><code>domain</code> returns <code>('justins-mbp-2.local.tld', ['145.15.168.192.in-addr.arpa'], ['192.168.15.145'])</code></p>
  
  <p><code>name</code> returns the first element of the tuple above and <code>ip</code> returns the last item of the tuple unwrapped from the collection.</p>
  
  <p>I'm looking for a return value of <code>localhost</code> or <code>localhost:8000</code>. I can work with either one.  </p>