def handle(self): # first send the info message self.authrand = authrand = os.urandom(4) self.write(msginfo(config.FBNAME, authrand)) while True: self.unpacker.feed(self.s.recv(BUFSIZ)) for opcode, data in self.unpacker: if opcode != OP_AUTH: self.error('First message was not AUTH.') raise BadClient() ident, rhash = readauth(data) self.authkey_check(ident, rhash) break while True: self.unpacker.feed(self.recv(BUFSIZ)) for opcode, data in self.unpacker: if opcode == OP_PUBLISH: self.do_publish(*readpublish(data)) elif opcode == OP_SUBSCRIBE: self.do_subscribe(*readsubscribe(data)) elif opcode == OP_UNSUBSCRIBE: self.do_unsubscribe(*readsubscribe(data)) else: self.error( "Unknown message type.", opcode=opcode, length=len(data), ) raise BadClient()
def test_readauth(self): ident, secret = readauth( b'\x05ident\xbf\xa9^\x11I\xcd\x9es' b'\x80\xfd\xfcaJW\tZ\xb7\x19\xc1\xb4' ) assert ident == 'ident' assert secret == ( b'\xbf\xa9^\x11I\xcd\x9es\x80\xfd\xfcaJW\tZ\xb7\x19\xc1\xb4' )
def messageReceived(self, opcode, data): if opcode == OP_ERROR: return self.onError(readerror(data)) elif opcode == OP_INFO: return self.onInfo(*readinfo(data)) elif opcode == OP_AUTH: return self.onAuth(*readauth(data)) elif opcode == OP_PUBLISH: return self.onPublish(*readpublish(data)) elif opcode == OP_SUBSCRIBE: return self.onSubscribe(*readsubscribe(data)) elif opcode == OP_UNSUBSCRIBE: return self.onUnsubscribe(*readunsubscribe(data)) # Can't recover from an unknown opcode, so drop connection self.protocolError('Unknown message opcode: {!r}'.format(opcode)) self.transport.loseConnection()