2017-07-21 8 views
0

twistedを使用してTCPサーバーを作成すると、TypeErrorが発生しました。 client.pyを起動した後にserver.pyと表示された場合は、エラートレースセクションに以下のエラーが表示されます。builtins.TypeError:Twistedで 'NoneType'オブジェクトを呼び出すことができない

このエラーについてはわかりません。しかし、エラーはserver.pyで発生すると確信しています。

server.py:

from twisted.internet import protocol, reactor 
    from time import ctime 

    PORT = 1234 
    class TserverProtocols(protocol.Protocol): 
     def connectionMade(self): 
      clnt = self.clnt = self.transport.getPeer().host 
      print('...connected from:', clnt) 
     def dataReceived(self, data): 
      self.transport.write(('[%s] %s' %(ctime(),data)).encode()) 

    factory = protocol.Factory() 
    factory.protocols = TserverProtocols 
    print('waiting for connetion...') 
    reactor.listenTCP(PORT, factory) 
    reactor.run() 

client.py:

from twisted.internet import protocol, reactor 

HOST = 'localhost' 
PORT = 1234 

class TSClntProtocol(protocol.Protocol): 
    def sendData(self): 
     data = input('>') 
     if data: 
      print('...sending %s' % data) 
      self.transport.write(data.encode()) 
     else: 
      self.transport.loseConnetion() 

    def connectionMade(self): 
     self.sendData() 

    def dataReceived(self, data): 
     print(data) 
     self.sendData() 

class TSCFactorty(protocol.ClientFactory): 
    protocol = TSClntProtocol 
    clientConnetionLost = clientConnectionFailed = lambda slef, connector, reason:reactor.stop() 

reactor.connectTCP(HOST, PORT, TSCFactorty()) 
reactor.run() 

エラートレースバック:

Unhandled Error 
Traceback (most recent call last): 
    File "/usr/local/lib/python3.5/dist-packages/twisted/python/log.py", line 86, in callWithContext 
    return context.call({ILogContext: newCtx}, func, *args, **kw) 
    File "/usr/local/lib/python3.5/dist-packages/twisted/python/context.py", line 122, in callWithContext 
    return self.currentContext().callWithContext(ctx, func, *args, **kw) 
    File "/usr/local/lib/python3.5/dist-packages/twisted/python/context.py", line 85, in callWithContext 
    return func(*args,**kw) 
    File "/usr/local/lib/python3.5/dist-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite 
    why = selectable.doRead() 
--- <exception caught here> --- 
    File "/usr/local/lib/python3.5/dist-packages/twisted/internet/tcp.py", line 1069, in doRead 
    protocol = self.factory.buildProtocol(self._buildAddr(addr)) 
    File "/usr/local/lib/python3.5/dist-packages/twisted/internet/protocol.py", line 133, in buildProtocol 
    p = self.protocol() 
builtins.TypeError: 'NoneType' object is not callable 

答えて

0

あなたが持っているいくつかの誤植あなたのコードで。あなたの質問について、あなたは

factory.protocols = TserverProtocols 

factory.protocol = TserverProtocols 
とserver.pyに置き換える必要があります
関連する問題