运行python程序错误,帮忙查看谢谢

Leon52177 发布于 2014/07/05 10:54
阅读 907
收藏 0
Exception happened during processing of request from ('127.0.0.1', 59614)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 638, in __init__
    self.handle()
  File "dns2.py", line 70, in handle
    dns = SinDNSFrame(data)
  File "dns2.py", line 53, in __init__
    self.query = SinDNSQuery(data[12:])
  File "dns2.py", line 24, in __init__
    self.name = self.name + chr(d)
TypeError: an integer is required

以下是问题补充:

@Leon52177:源代码 ''' Created on 2012-10-15 @author: RobinTang ''' #import socketserver #Python 3 import SocketServer #Python 2 import struct # DNS Query class SinDNSQuery: def __init__(self, data): i = 1 self.name = '' while True: d = data[i] if d == 0: break; if d < 32: self.name = self.name + '.' else: self.name = self.name + chr(d) i = i + 1 self.querybytes = data[0:i + 1] (self.type, self.classify) = struct.unpack('>HH', data[i + 1:i + 5]) self.len = i + 5 def getbytes(self): return self.querybytes + struct.pack('>HH', self.type, self.classify) # DNS Answer RRS # this class is also can be use as Authority RRS or Additional RRS class SinDNSAnswer: def __init__(self, ip): self.name = 49164 self.type = 1 self.classify = 1 self.timetolive = 190 self.datalength = 4 self.ip = ip def getbytes(self): res = struct.pack('>HHHLH', self.name, self.type, self.classify, self.timetolive, self.datalength) s = self.ip.split('.') res = res + struct.pack('BBBB', int(s[0]), int(s[1]), int(s[2]), int(s[3])) return res # DNS frame # must initialized by a DNS query frame class SinDNSFrame: def __init__(self, data): (self.id, self.flags, self.quests, self.answers, self.author, self.addition) = struct.unpack('>HHHHHH', data[0:12]) self.query = SinDNSQuery(data[12:]) def getname(self): return self.query.name def setip(self, ip): self.answer = SinDNSAnswer(ip) self.answers = 1 self.flags = 33152 def getbytes(self): res = struct.pack('>HHHHHH', self.id, self.flags, self.quests, self.answers, self.author, self.addition) res = res + self.query.getbytes() if self.answers != 0: res = res + self.answer.getbytes() return res # A UDPHandler to handle DNS query class SinDNSUDPHandler(SocketServer.BaseRequestHandler): def handle(self): data = self.request[0].strip() dns = SinDNSFrame(data) socket = self.request[1] namemap = SinDNSServer.namemap if(dns.query.type==1): # If this is query a A record, then response it name = dns.getname(); if namemap.__contains__(name): # If have record, response it dns.setip(namemap[name]) socket.sendto(dns.getbytes(), self.client_address) elif namemap.__contains__('*'): # Response default address dns.setip(namemap['*']) socket.sendto(dns.getbytes(), self.client_address) else: # ignore it socket.sendto(data, self.client_address) else: # If this is not query a A record, ignore it socket.sendto(data, self.client_address) # DNS Server # It only support A record query # user it, U can create a simple DNS server class SinDNSServer: def __init__(self, port=53): SinDNSServer.namemap = {} self.port = port def addname(self, name, ip): SinDNSServer.namemap[name] = ip def start(self): HOST, PORT = "10.18.10.11", self.port server = SocketServer.UDPServer((HOST, PORT), SinDNSUDPHandler) server.serve_forever() # Now, test it if __name__ == "__main__": sev = SinDNSServer() sev.addname('www.aa.com', '192.168.0.1') # add a A record sev.addname('www.bb.com', '192.168.0.2') # add a A record sev.addname('*', '0.0.0.0') # default address sev.start() # start DNS server # Now, U can use "nslookup" command to test it # Such as "nslookup www.aa.com" (2014/07/05 10:55)
加载中
0
22too
22too
你的代码贴的太奇葩了!
0
L
Leon52177
'''
Created on 2012-10-15
 
@author : RobinTang
'''
 
 
#import socketserver      #Python 3
import SocketServer      #Python 2
import struct
 
# DNS Query
class SinDNSQuery:
    def __init__(self, data):
        i = 1
        self.name = ''
        while True:
            d = data[i]
            if d == 0:
                break;
            if d < 32:
                self.name = self.name + '.'
            else:
                self.name = self.name + chr(d)
            i = i + 1
        self.querybytes = data[0:i + 1]
        (self.type, self.classify) = struct.unpack('>HH', data[i + 1:i + 5])
        self.len = i + 5
    def getbytes(self):
        return self.querybytes + struct.pack('>HH', self.type, self.classify)
 
# DNS Answer RRS
# this class is also can be use as Authority RRS or Additional RRS
class SinDNSAnswer:
    def __init__(self, ip):
        self.name = 49164
        self.type = 1
        self.classify = 1
        self.timetolive = 190
        self.datalength = 4
        self.ip = ip
    def getbytes(self):
        res = struct.pack('>HHHLH', self.name, self.type, self.classify, self.timetolive, self.datalength)
        s = self.ip.split('.')
        res = res + struct.pack('BBBB', int(s[0]), int(s[1]), int(s[2]), int(s[3]))
        return res
 
# DNS frame
# must initialized by a DNS query frame
class SinDNSFrame:
    def __init__(self, data):
        (self.id, self.flags, self.quests, self.answers, self.author, self.addition) = struct.unpack('>HHHHHH', data[0:12])
        self.query = SinDNSQuery(data[12:])
    def getname(self):
        return self.query.name
    def setip(self, ip):
        self.answer = SinDNSAnswer(ip)
        self.answers = 1
        self.flags = 33152
    def getbytes(self):
        res = struct.pack('>HHHHHH', self.id, self.flags, self.quests, self.answers, self.author, self.addition)
        res = res + self.query.getbytes()
        if self.answers != 0:
            res = res + self.answer.getbytes()
        return res
# A UDPHandler to handle DNS query
class SinDNSUDPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        data = self.request[0].strip()
        dns = SinDNSFrame(data)
        socket = self.request[1]
        namemap = SinDNSServer.namemap
        if(dns.query.type==1):
            # If this is query a A record, then response it
             
            name = dns.getname();
            if namemap.__contains__(name):
                # If have record, response it
                dns.setip(namemap[name])
                socket.sendto(dns.getbytes(), self.client_address)
            elif namemap.__contains__('*'):
                # Response default address
                dns.setip(namemap['*'])
                socket.sendto(dns.getbytes(), self.client_address)
            else:
                # ignore it
                socket.sendto(data, self.client_address)
        else:
            # If this is not query a A record, ignore it
            socket.sendto(data, self.client_address)
 
# DNS Server
# It only support A record query
# user it, U can create a simple DNS server
class SinDNSServer:
    def __init__(self, port=53):
        SinDNSServer.namemap = {}
        self.port = port
    def addname(self, name, ip):
        SinDNSServer.namemap[name] = ip
    def start(self):
        HOST, PORT = "10.18.10.11", self.port
        server = SocketServer.UDPServer((HOST, PORT), SinDNSUDPHandler)
        server.serve_forever()
 
# Now, test it
if __name__ == "__main__":
    sev = SinDNSServer()
    sev.addname('www.aa.com', '192.168.0.1')    # add a A record
    sev.addname('www.bb.com', '192.168.0.2')    # add a A record
    sev.addname('*', '0.0.0.0') # default address
    sev.start() # start DNS server
 
# Now, U can use "nslookup" command to test it
# Such as "nslookup www.aa.com"
0
22too
22too
#import socketserver      #Python 3 
import SocketServer      #Python 2 
import struct 
  
# DNS Query 
class SinDNSQuery: 
    def __init__(self, data): 
        i = 1 
        self.name = '' 
        while True: 
            d = data[i] 
            if d == 0: 
                break; 
            if d < 32: 
                self.name = self.name + '.' 
            else: 
                self.name = self.name + chr(d) 
            i = i + 1 
        self.querybytes = data[0:i + 1] 
        (self.type, self.classify) = struct.unpack('>HH', data[i + 1:i + 5]) 
        self.len = i + 5 
    def getbytes(self): 
        return self.querybytes + struct.pack('>HH', self.type, self.classify) 
  
# DNS Answer RRS 
# this class is also can be use as Authority RRS or Additional RRS 
class SinDNSAnswer: 
    def __init__(self, ip): 
        self.name = 49164 
        self.type = 1 
        self.classify = 1 
        self.timetolive = 190 
        self.datalength = 4 
        self.ip = ip 
    def getbytes(self): 
        res = struct.pack('>HHHLH', self.name, self.type, self.classify, self.timetolive, self.datalength) 
        s = self.ip.split('.') 
        res = res + struct.pack('BBBB', int(s[0]), int(s[1]), int(s[2]), int(s[3])) 
        return res 
  
# DNS frame 
# must initialized by a DNS query frame 
class SinDNSFrame: 
    def __init__(self, data): 
        (self.id, self.flags, self.quests, self.answers, self.author, self.addition) = struct.unpack('>HHHHHH', data[0:12]) 
        self.query = SinDNSQuery(data[12:]) 
    def getname(self): 
        return self.query.name 
    def setip(self, ip): 
        self.answer = SinDNSAnswer(ip) 
        self.answers = 1 
        self.flags = 33152 
    def getbytes(self): 
        res = struct.pack('>HHHHHH', self.id, self.flags, self.quests, self.answers, self.author, self.addition) 
        res = res + self.query.getbytes() 
        if self.answers != 0: 
            res = res + self.answer.getbytes() 
        return res 
# A UDPHandler to handle DNS query 
class SinDNSUDPHandler(SocketServer.BaseRequestHandler): 
    def handle(self): 
        data = self.request[0].strip() 
        dns = SinDNSFrame(data) 
        socket = self.request[1] 
        namemap = SinDNSServer.namemap 
        if(dns.query.type==1): 
            # If this is query a A record, then response it 
              
            name = dns.getname(); 
            if namemap.__contains__(name): 
                # If have record, response it 
                dns.setip(namemap[name]) 
                socket.sendto(dns.getbytes(), self.client_address) 
            elif namemap.__contains__('*'): 
                # Response default address 
                dns.setip(namemap['*']) 
                socket.sendto(dns.getbytes(), self.client_address) 
            else: 
                # ignore it 
                socket.sendto(data, self.client_address) 
        else: 
            # If this is not query a A record, ignore it 
            socket.sendto(data, self.client_address) 
  
# DNS Server 
# It only support A record query 
# user it, U can create a simple DNS server 
class SinDNSServer: 
    def __init__(self, port=53): 
        SinDNSServer.namemap = {} 
        self.port = port 
    def addname(self, name, ip): 
        SinDNSServer.namemap[name] = ip 
    def start(self): 
        HOST, PORT = "10.18.10.11", self.port 
        server = SocketServer.UDPServer((HOST, PORT), SinDNSUDPHandler) 
        server.serve_forever() 
  
# Now, test it 
if __name__ == "__main__": 
    sev = SinDNSServer() 
    sev.addname('www.aa.com', '192.168.0.1')    # add a A record 
    sev.addname('www.bb.com', '192.168.0.2')    # add a A record 
    sev.addname('*', '0.0.0.0') # default address 
    sev.start() # start DNS server 
  
# Now, U can use "nslookup" command to test it 
# Such as "nslookup www.aa.com"



0
Xsank
Xsank
self.name = self.name + chr(d)

TypeError: an integer is required

这个提示已经非常明显了,那个d有时候不是整数



0
L
Leon52177
有人说是跟编码有关系,chr类型转完之后就成乱码了
0
L
Leon52177
找到问题了,我用python3.3.3执行这个文件就好了,2.7版本的不知道为何不行
返回顶部
顶部