我正在运行一个扭曲的应用程序大约一年了。这一切都运作良好,直到2月底。从那时起,我无法在客户端和服务器之间传输任何数据。我根据我的真实代码编写了一个小例子:
服务器:
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 Jessie上使用Python 3.4.2在两个单独的Raspberry Pis上运行应用程序时,这是服务器的输出:
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”)?我做错了什么?
非常感谢您的帮助。
答案 0 :(得分:1)