2
私は今では1年くらいの間、少しねじれたアプリケーションを実行しています。それはすべて2月末までうまくいった。それ以来、クライアントとサーバーの間でデータを転送することはできません。私は私の実際のコードに基づいて、少し例を準備:Twisted 16 LineReceiver:コールバックは一切起きません
サーバー:
from twisted.python.filepath import FilePath
from twisted.logger import Logger, textFileLogObserver, globalLogPublisher, globalLogBeginner
from twisted.internet.endpoints import SSL4ServerEndpoint
from twisted.internet.ssl import PrivateCertificate, Certificate
from twisted.internet.defer import Deferred
from twisted.internet.task import react
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
import codecs
import sqlite3
import os
import sys
globalLogBeginner.beginLoggingTo([textFileLogObserver(sys.stdout)])
loggy = Logger()
class Ex_Factory(Factory):
def __init__(self, loggy):
self.loggy = loggy
def buildProtocol(self, addr):
return Ex_Protocol(self.loggy)
class Ex_Protocol(LineReceiver):
def dataReceived(self,data):
self.loggy.info('mmm')
def connectionMade(self):
self.sendLine(b'hello')
self.loggy.info('ConMade')
def __init__(self, loggy):
self.loggy = loggy
self.loggy.info('Works')
def lineReceived(self, data):
self.loggy.info('I am not called! But why?')
def main(reactor):
#Set up a SSL-Endpoint
pemBytes = FilePath(b"private.pem").getContent()
certificateAuthority = Certificate.loadPEM(pemBytes)
myCertificate = PrivateCertificate.loadPEM(pemBytes)
serverEndpoint = SSL4ServerEndpoint(
reactor, 1234, myCertificate.options(certificateAuthority)
)
serverEndpoint.listen(Ex_Factory(loggy))
return Deferred()
react(main, [])
クライアント:
dir="/working_dir"
certname="private.pem"
from twisted.python.filepath import FilePath
from twisted.internet.endpoints import SSL4ClientEndpoint
from twisted.internet.ssl import (PrivateCertificate, Certificate, optionsForClientTLS)
from twisted.internet.defer import Deferred, inlineCallbacks
from twisted.internet.task import react
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
import os
class SendAnyData(LineReceiver):
deferred = Deferred()
def connectionMade(self):
print('ConMade!')
self.sendLine(b"START")
self.transport.write(b"START\r\n")
def connectionLost(self, reason):
print('ConLost!')
self.deferred.callback(None)
def lineReceived(self, data):
line = data.decode("UTF-8")
@inlineCallbacks
def main(reactor):
def getServerandPort(i):
return ("1.2.3.4",1234)
pem = FilePath(b"Certificates/"+certname.encode("UTF-8")).getContent()
caPem = FilePath(b"Certificates/ca-private-cert.pem").getContent()
host, port = getServerandPort(0)
clientEndpoint = SSL4ClientEndpoint(
reactor, host, port,
optionsForClientTLS(u"ABC", Certificate.loadPEM(caPem),
PrivateCertificate.loadPEM(pem)),
)
factory = Factory.forProtocol(SendAnyData)
proto = yield clientEndpoint.connect(factory)
yield proto.deferred
os.chdir(dir)
react(main)
私は、DebianでPython 3.4.2を使用して、2つの別々のラズベリー小僧上でアプリケーションを実行すると、 Jessie、これはサーバーからの出力です。
2016-04-09T11:44:01+0200 [-] Ex_Factory (TLS) starting on 1234
2016-04-09T11:44:01+0200 [__main__.Ex_Factory#info] Starting factory <__main__.Ex_Factory object at 0xb5f2ee50>
2016-04-09T11:44:49+0200 [__main__#info] Works
2016-04-09T11:44:49+0200 [__main__#info] ConMade
^C2016-04-09T11:46:54+0200 [-] Received SIGINT, shutting down.
2016-04-09T11:46:54+0200 [twisted.protocols.tls.TLSMemoryBIOFactory] (TLS Port 1234 Closed)
2016-04-09T11:46:54+0200 [__main__.Ex_Factory#info] Stopping factory <__main__.Ex_Factory object at 0xb5f2ee50>
2016-04-09T11:46:54+0200 [-] Main loop terminated.
クライアントからサーバーに送信されました(「START」)。私は間違って何をしていますか?
ありがとうございました。