Skip to content

Instantly share code, notes, and snippets.

@hahastudio
Created September 29, 2012 10:50
Show Gist options
  • Select an option

  • Save hahastudio/3803682 to your computer and use it in GitHub Desktop.

Select an option

Save hahastudio/3803682 to your computer and use it in GitHub Desktop.

Revisions

  1. @phuslu phuslu revised this gist Mar 13, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions dns_resolve.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    import os
    import re
    import socket
    import struct

    def dns_resolve(host, dnsserver):
    assert isinstance(host, basestring) and isinstance(dnsserver, basestring)
  2. @phuslu phuslu revised this gist Mar 13, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions dns_resolve.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    import os
    import socket

    def dns_resolve(host, dnsserver):
    assert isinstance(host, basestring) and isinstance(dnsserver, basestring)
    index = os.urandom(2)
  3. @phuslu phuslu created this gist Mar 12, 2012.
    24 changes: 24 additions & 0 deletions dns_resolve.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    def dns_resolve(host, dnsserver):
    assert isinstance(host, basestring) and isinstance(dnsserver, basestring)
    index = os.urandom(2)
    hoststr = ''.join(chr(len(x))+x for x in host.split('.'))
    data = '%s\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00%s\x00\x00\x01\x00\x01' % (index, hoststr)
    data = struct.pack('!H', len(data)) + data
    address_family = {True:socket.AF_INET6, False:socket.AF_INET}[':' in dnsserver]
    sock = None
    try:
    sock = socket.socket(family=address_family)
    sock.connect((dnsserver, 53))
    sock.sendall(data)
    rfile = sock.makefile('rb')
    size = struct.unpack('!H', rfile.read(2))[0]
    data = rfile.read(size)
    iplist = re.findall('\xC0.\x00\x01\x00\x01.{6}(.{4})', data)
    return ['.'.join(str(ord(x)) for x in s) for s in iplist]
    except Exception, e:
    raise
    finally:
    if sock:
    sock.close()

    print dns_resolve('www.youtube.com', '8.8.8.8')