title: Can't get Custom DNS server working in Python
body: <p>I'm having real trouble getting python to use a custom dns server. 
  I have followed this <a href="https://stackoverflow.com/questions/2236498/tell-urllib2-to-use-custom-dns/2237040#2237040">Tell urllib2 to use custom DNS</a> 
  If I don't specify a self.host and self.port, it will go through without blocking. </p>
  
  <p>Here is the code:</p>
  
  <pre><code>import urllib2
  import httplib
  import socket
  class MyHTTPConnection (httplib.HTTPConnection):
      def connect (self):
          if self.host == 'www.porn.com':
              self.host = '208.67.222.123' #OpenDNS FamilyShield
              self.port = 53
          self.sock = socket.create_connection ((self.host, self.port))
  class MyHTTPHandler (urllib2.HTTPHandler):
      def http_open (self, req):
          return self.do_open (MyHTTPConnection, req)
  
  opener = urllib2.build_opener(MyHTTPHandler)
  urllib2.install_opener (opener)
  f = urllib2.urlopen ('http://www.porn.com/videos/anime-toon.html')
  data = f.read ()
  print data
  </code></pre>
  
  <p>I keep getting a "raise BadStatusLine(line)" error</p>
  
  <p>Error log:</p>
  
  <pre><code>Traceback (most recent call last):
    File "K:\Desktop\rte\dns2.py", line 16, in &lt;module&gt;
      f = urllib2.urlopen ('http://www.porn.com/videos/anime-toon.html')
    File "C:\Python27\lib\urllib2.py", line 126, in urlopen
      return _opener.open(url, data, timeout)
    File "C:\Python27\lib\urllib2.py", line 394, in open
      response = self._open(req, data)
    File "C:\Python27\lib\urllib2.py", line 412, in _open
      '_open', req)
    File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
      result = func(*args)
    File "K:\Desktop\rte\dns2.py", line 12, in http_open
      return self.do_open (MyHTTPConnection, req)
    File "C:\Python27\lib\urllib2.py", line 1170, in do_open
      r = h.getresponse(buffering=True)
    File "C:\Python27\lib\httplib.py", line 1027, in getresponse
      response.begin()
    File "C:\Python27\lib\httplib.py", line 407, in begin
      version, status, reason = self._read_status()
    File "C:\Python27\lib\httplib.py", line 371, in _read_status
      raise BadStatusLine(line)
  BadStatusLine: ''
  </code></pre>
  
  <p>EDIT: Going on isedev response, that I was going about it the wrong way.</p>
  
  <p>It doesn't seem to register with urllib2 the changes to the namesservers</p>
  
  <pre><code>import dns.resolver
  import urllib2
  
  resolver = dns.resolver.Resolver()
  resolver.nameservers = ['208.67.222.123']
  answer = resolver.query('www.porn.com','A')
  web_url = 'http://www.porn.com/videos/anime-toon.html'
  req1 = urllib2.Request(web_url)
  req1.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
  response1 = urllib2.urlopen(req1)
  html=response1.read()
  print html
  </code></pre>