2016-07-16 1 views
2

git hubのtxtnettoolsパッケージから次のコードを取得しました。コードが大部分不完全なので、実際にコードを作成する方法を理解しようとしています。sendEcho()メソッド私は適切なラインを追加しました。Twisted ExtendSelected Reactorエラー

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エラー」またはそれ以外の文字は、文字通り解決策やチャッタがありません。おかげ

編集:ここでは、ソース・プロジェクトが https://github.com/oberstet/txnettools

答えて

1

答えは私がピンガークラスにアクセスしたかには全くだったGitHubのです。コードの修正ラインは、代わりの

reactor.callWhenRunning(Pinger().sendEcho) 

次のようになります。

あなたはどのように、どのように新しいものを作成するときに、それ自体を延期する反応器に指定する必要が
reactor.Pinger.sendEcho() 

関連する問題