Search code examples
pythontwistedreactor

Twisted ExtendSelected Reactor error


I pulled the following code from the txtnettools package on git hub the code is largely incomplete so I am trying to figure out how to actually make the code send packets I need to call I think the sendEcho() method as a reactor so I added the appropriate line.

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() however when I run this script I get the following error.

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

Googling "ExtendedSelectRector error" or anything close yeilds literally no solutions or chatter. Thanks

Edit: here is the source projects github https://github.com/oberstet/txnettools


Solution

  • The answer was totally in how I was accessing the Pinger class. The corrected line of code would be

     reactor.callWhenRunning(Pinger().sendEcho)
    

    Instead of:

     reactor.Pinger.sendEcho()
    

    You need to specify to the reactor how when and how to defer itself when you create a new one.