无法使用Python工作的自定义DNS服务器

时间:2013-01-27 22:00:27

标签: python sockets dns urllib2 httplib

我在让python使用自定义DNS服务器时遇到了麻烦。 我跟着这个Tell urllib2 to use custom DNS 如果我没有指定self.host和self.port,它将无阻塞地通过。

以下是代码:

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

我不断收到“提高BadStatusLine(line)”错误

错误日志:

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: ''

编辑:继续isedev回应,我的方式错误。

似乎没有向urllib2注册名称服务器的更改

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 个答案:

答案 0 :(得分:2)

我认为你误解了你所提到的“自定义DNS”答案中正在做什么。该解决方案中给出的示例实际上并未设置自定义DNS服务器 - MyResolver类仅作为示例提供,并为'news.bbc.co.uk'执行硬编码的名称到IP。

那么你的代码实际上正在做的是将HTTP请求重定向到'www.porn.com'(端口80)到OpenDNS Family Shield DNS服务器(在端口53上)......这显然会导致错误你得到了。

所以你需要做的就是替换:

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

使用直接针对所选DNS服务器解析“www.porn.com”的代码(例如使用dnspython)。

假设您已安装dnspython软件包,您可以执行以下操作:

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

此代码返回'404 - not found',网络跟踪显示对'hit-adult.opendns.com'的HTTP请求,这是'www.porn.com'在使用'208.67.222.123'名称服务器时解决的问题:

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.

以上只是一个例子。真正的代码需要错误检查等...