【问题标题】:Twisted ExtendSelected Reactor errorTwisted ExtendSelected Reactor 错误
【发布时间】:2016-07-16 20:23:11
【问题描述】:

我从 git hub 上的 txtnettools 包中提取了以下代码,该代码基本上不完整,所以我试图弄清楚如何实际使代码发送我需要调用的数据包我认为 sendEcho() 方法作为反应器所以我添加了适当的行。

from random import randint
import socket
import struct

from twisted.internet.protocol import DatagramProtocol

# needs to be run with sudo, so let's add the dev path
import sys
sys.path.insert(0, ".")

from txnet.icmp import *
from txnet.reactor import reactor


UDP_PORT_MIN = 33434
UDP_PORT_MAX = 33534


def get_remote_port():
    return randint(UDP_PORT_MIN, UDP_PORT_MAX)


class Pinger(ICMP):

      def sendEcho(self):
          print "Sending echo ..."
          src = "192.168.1.1"
          print src
          #dst = "127.0.0.1"
          #dst = "192.168.1.1"
          #dst = "192.168.100.1"
          dst = "74.125.45.100"
          self.transport.connect(dst, get_remote_port())
          # Construct a ping packet (with useless payload data).
          packet = Packet(src=src, dst=dst, type=ECHO_REQUEST, payload="txNetTools ping")
          raw = packet.getDatagram()
          self.transport.write(packet.getDatagram())

     def startProtocol(self):
         print "Transport is:", self.transport
         print "Transport class is:", self.transport.__class__
         print "self is:", self
         self.sendEcho()

     def connectionRefused(self):
         print "Connection refused ..."
         print "Host:", self.transport.getHost()
         print "Remote host:", self.transport._connectedAddr
         print "Connected:", self.transport.connected
         print "Disconnected:", self.transport.disconnected
         print "Data buffer:", self.transport.dataBuffer

 reactor.Pinger.sendEcho() #Throwing error 
 reactor.listenICMP(0, Pinger())
 reactor.run()

Rector.pinger.sendEcho() 但是,当我运行此脚本时,我收到以下错误。

 Traceback (most recent call last):
   File "ping.py", line 54, in <module>
   reactor.Pinger.sendEcho()
 AttributeError: 'ExtendedSelectReactor' object has no attribute 'Pinger'

谷歌搜索“ExtendedSelectRector 错误”或任何接近的内容实际上不会产生任何解决方案或喋喋不休。谢谢

编辑:这里是源项目 github https://github.com/oberstet/txnettools

【问题讨论】:

    标签: python twisted reactor


    【解决方案1】:

    答案完全在于我是如何访问 Pinger 类的。更正的代码行将是

     reactor.callWhenRunning(Pinger().sendEcho)
    

    代替:

     reactor.Pinger.sendEcho()
    

    您需要向反应器指定在创建新反应器时如何以及如何延迟自身。

    【讨论】:

      猜你喜欢
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 2015-01-22
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      相关资源
      最近更新 更多