Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:46:10+00:00 2026-06-17T21:46:10+00:00

I’m having real trouble getting python to use a custom dns server. I have

  • 0

I’m having real trouble getting python to use a custom dns server.
I have followed this Tell urllib2 to use custom DNS
If I don’t specify a self.host and self.port, it will go through without blocking.

Here is the 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

I keep getting a “raise BadStatusLine(line)” error

Error log:

Traceback (most recent call last):
  File "K:\Desktop\rte\dns2.py", line 16, in <module>
    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: ''

EDIT: Going on isedev response, that I was going about it the wrong way.

It doesn’t seem to register with urllib2 the changes to the namesservers

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:46:11+00:00Added an answer on June 17, 2026 at 9:46 pm

    I think you’ve misunderstood what’s being done in the “Custom DNS” answer you refer to. The example given in that solution is not in fact setting up a custom DNS server – the MyResolver class is given as example only and performs a hard-coded name-to-IP for ‘news.bbc.co.uk’.

    So what your code is actually doing is redirecting an HTTP request to ‘www.porn.com’ (port 80) to the OpenDNS Family Shield DNS server (on port 53)… which will obviously lead to the error you’re getting.

    So what you need to do is replace:

    if self.host == 'www.porn.com':
        self.host = '208.67.222.123' #OpenDNS FamilyShield
        self.port = 53
    

    with code that actually resolves ‘www.porn.com’ against the chosen DNS server directly (using dnspython for instance).

    Assuming you’ve got the dnspython package installed, you could do something like:

    import urllib2
    import httplib
    import socket
    import dns.resolver
    
    class MyHTTPConnection (httplib.HTTPConnection):
        def connect (self):
            if self.host == 'www.porn.com':
                resolver = dns.resolver.Resolver()
                resolver.nameservers = ['208.67.222.123']
                answer = resolver.query(self.host,'A')
                self.host = answer.rrset.items[0].address
            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
    

    This code returns ‘404 – not found’ and network trace shows HTTP request to ‘hit-adult.opendns.com’, which is what ‘www.porn.com’ resolves to when using the ‘208.67.222.123’ nameserver:

    dig @208.67.222.123 www.porn.com A
    ;; ANSWER SECTION:
    www.porn.com.           0       IN      A       67.215.65.130
    
    nslookup 67.215.65.130
    130.65.215.67.in-addr.arpa      name = hit-adult.opendns.com.
    

    The above is an example only. Real code would require error checking, etc…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.