V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
cmsee
V2EX  ›  Python

可以帮我修改成一个函数吗?我才接触python.谢谢大家。

  •  
  •   cmsee · 2012-11-04 16:27:41 +08:00 · 3620 次点击
    这是一个创建于 4384 天前的主题,其中的信息可能已经有所发展或是发生改变。
    ###############file QueryDNS.py#################################
    #coding: GBK
    #Get DNS answer
    #详情见RFC 1035
    import os, sys
    import socket
    import struct
    import random
    from domaintobyte import domaintobyte, bytetodomain
    #DHOST = '208.67.222.222' #DNS 服务器的地址
    DHOST = '8.8.8.8' #DNS 服务器的地址
    DPORT = 53 #默认端口是53
    LHOST = ''
    LPORT = 10001 #绑定到的本地端口
    TIMEOUT = 3.0 #超时设置为3秒
    ##数据包整体的格式
    ## +---------------------+
    ## | Header |
    ## +---------------------+
    ## | Question | the question for the name server
    ## +---------------------+
    ## | Answer | RRs answering the question
    ## +---------------------+
    ## | Authority | RRs pointing toward an authority
    ## +---------------------+
    ## | Additional | RRs holding additional information
    ## +---------------------+
    def QueryDNS(domain):
    TID = random.randint(-32768, 32767)
    Flags = 0x0100
    Questions = 0x0001
    AnswerRRs = 0x0000
    AuthorityRRs = 0x0000
    AdditionalRRs = 0x0000
    TIDCHARS = struct.pack('!h', TID)
    domainbyte = domaintobyte(domain)
    #TYPE value and meaning
    #A 1 a host address
    #NS 2 an authoritative name server
    #MD 3 a mail destination (Obsolete - use MX)
    #MF 4 a mail forwarder (Obsolete - use MX)
    #CNAME 5 the canonical name for an alias
    SEARCHTYPE = 0x0001
    #Class 一般为 1 指 Internet
    SEARCHCLASS = 0x0001
    #构造请求报文
    Bufhead = struct.pack('!hhhhhh', TID, Flags, Questions, AnswerRRs, AuthorityRRs, AdditionalRRs)
    Buftail = struct.pack('!hh', SEARCHTYPE, SEARCHCLASS)
    Buf = Bufhead + domainbyte + Buftail
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.settimeout(TIMEOUT) #设置超时时间
    s.bind((LHOST,LPORT))

    s.sendto(Buf,(DHOST, DPORT))
    print 'Send [OK]'

    try:
    data, addr = s.recvfrom(1024)
    except socket.timeout:
    s.close()
    print u'响应超时'
    return

    s.close()
    print 'From', addr
    print 'Receved', repr(data)

    if data[0:2] == TIDCHARS:

    flags = struct.unpack('!h', data[2:4])[0]
    errormsg = flags & 0x000F
    if errormsg != 0:

    return

    answerRRs = struct.unpack('!h', data[6:8])[0]
    bitflags = 12;



    while data[bitflags] != '\x00':
    bitflags += 1
    bitflags += 1
    bitflags += 2
    bitflags += 2


    i = 0

    while i < answerRRs:

    bitflags += 2
    if data[bitflags:bitflags+2] == '\x00\x05':


    bitflags += 8
    rdatalength = struct.unpack('!h', data[bitflags:bitflags+2])[0]

    bitflags += 2
    #得到rdata中的全部域名
    fullRecord = GetFullName(bitflags, data)
    bitflags += rdatalength
    print 'CNAME:', bytetodomain(fullRecord)

    i += 1

    def GetFullName(offset, Buf):
    fullRecord = ''
    oneChar = struct.unpack('!B', Buf[offset:offset+1])[0]
    #print oneChar
    if oneChar & 0xc0 == 0xc0 : #指针
    jump = struct.unpack('!h', Buf[offset:offset+2])[0]
    jump = jump & 0x3FFF #指针指向的地址
    fullRecord += GetFullName(jump, Buf)
    elif oneChar == 0 : #域名以\0结束
    return '\x00'
    else : #域名部分
    fullRecord += Buf[offset:offset+oneChar+1]
    fullRecord += GetFullName(offset+oneChar+1, Buf)

    return fullRecord


    if __name__ == "__main__":
    print 'main start...'
    if len(sys.argv) < 2:
    print "Usage:QueryDNS.py www.google.cn"
    elif len(sys.argv) == 2:
    DHOST = '8.8.8.8' #DNS 服务器的地址
    else:
    DHOST = sys.argv[2]
    try:
    domain = sys.argv[1]
    QueryDNS(domain)
    except Exception, e:
    logging.error("exit:error:%s" %e)
    print 'main over'

    ----------------------------------------------

    修改成了

    def _get_response(self, data ,CorF):
    con = 0
    rspdata = None
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.connect((self.server.servers[(not CorF) and 1 or 0], 53))
    while not rspdata and con <4:
    try:
    con += 1
    sock.sendall(data)
    sock.settimeout(3)
    rspdata = sock.recv(65535)
    except:
    rspdata = None
    sock.close()
    return rspdata


    这样的就可以了。获取域名的CNAME.
    。。
    2 条回复    1970-01-01 08:00:00 +08:00
    hyq
        1
    hyq  
       2012-11-04 16:57:44 +08:00
    我不是来回答问题的,我向楼主提个建议,贴代码的时候,可以使用github的gist
    http://gist.github.com
    贴上来的时候,把https换成http就行
    cmsee
        2
    cmsee  
    OP
       2012-11-04 17:14:37 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1911 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 16:23 · PVG 00:23 · LAX 08:23 · JFK 11:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.