1

我写了一个 Twisted Server 和一个常规的套接字客户端(因为客户端没有安装 Twisted)。部分服务器代码如下

import time
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor

class Server:
    def method(self, addr,port,sock):
        command = 'tcpdump -i any  \'src ' +str(addr) +' and port 80\' -w '+str(addr)+ '_download_' +str(time.time())+'.txt &'
        os.system(command)
        sock.transport.write('ok')


class Echo(Protocol):
    def dataReceived(self,data):
        s = Server()
        #extract client ip from the method self.transport.getPeer()
        #extract client port from the data received 
        print data
        s.method(client_ip, port, self)#the client ip and port are extracted from the request received

def main():
    f = Factory()
    f.protocol = Echo
    reactor.listenTCP(33456, f)
    reactor.run()

if __name__ == '__main__':
    main()

此方法适用于第一个客户端请求,但在第二个请求时失败并出现以下错误

exceptions.AttributeError: 'datetime.timedelta' object has no attribute 'time'连同命令的行号(我正在使用 time.time() 方法来获取文件名)

谁能指出我做错了什么的正确方向?或者为什么它会抛出错误?

编辑:

我没有在这里发布完整的代码,但回溯是

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 586, in _doReadOrWrite
    why = selectable.doRead()
  File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 199, in doRead
    rval = self.protocol.dataReceived(data)
  File "dc.py", line 191, in dataReceived
    dc.multiQ(client_ip,multiq_port,self)
  File "dc.py", line 73, in multiQ
    command = 'tcpdump -i any  \'src ' +str(addr) +' and port 80\' -w '+str(addr)+ '_download_' +str(time.time())+'.txt &'#change any to eth0
exceptions.AttributeError: 'datetime.timedelta' object has no attribute 'time'
4

0 回答 0