Assistance please with WampCra

#1

Hi there. I’m trying to use WampCra from android to python. I am having some issues and I am not sure whether it is due to compatibility between client and server. I’m running inside a virtualenv on the python sid

I’m trying to follow the demo here:

https://github.com/tavendo/AutobahnAndroid/blob/master/Demo/CRAClient/src/de/tavendo/autobahn/craclient/CRAClientActivity.java

Note that on line 96 I had to use Wamp.ConnectionHandler() instead of WampCra.ConnectionHandler() because the latter does not resolve for me.

Originally I am using AutobahnAndroid 0.7.2 (git trunk) and AutobahnPython 0.6.5 (version from pip). When I try to connect on these versions I get a connection failure with server error 400 (this server only speaks wamp).

When I build and install the git truck AutobahnPython in my virtualenv (using setup.py with virtualenv active and using the python binary for my virtualenv) I get the following import error:

import autobahn
Traceback (most recent call last):
File “”, line 1, in
File “$HOME/Development/virtualenvs/qserver/local/lib/python2.7/site-packages/autobahn-0.7.0-py2.7.egg/autobahn/init.py”, line 38, in
import wamp2
File “$HOME/Development/virtualenvs/qserver/local/lib/python2.7/site-packages/autobahn-0.7.0-py2.7.egg/autobahn/wamp2/init.py”, line 27, in
import websocket
File “$HOME/Development/virtualenvs/qserver/local/lib/python2.7/site-packages/autobahn-0.7.0-py2.7.egg/autobahn/wamp2/websocket.py”, line 34, in
from serializer import WampJsonSerializer, WampMsgPackSerializer
ImportError: cannot import name WampMsgPackSerializer

Here is my code. Please note that I am still just trying to get a connection. The authentication code might not be exactly right:

+============ ANDROID

(called directly from my onCreate)
private void startAutobahnClient() {
final String wsuri = “ws://6.6.0.132:9000”;
Log.i(TAG,“Connecting to server”);
mConnection.connect(wsuri, new Wamp.ConnectionHandler() {
@Override
public void onOpen() {
Log.i(TAG,“Connected to “+wsuri+”\n”);
mConnection.authenticate(new WampCra.AuthHandler() {

                @Override
                public void onAuthSuccess(Object arg0) {
                    Log.i(TAG,"Authenticated!");
                }
               
                @Override
                public void onAuthError(String arg0, String arg1) {
                    Log.e(TAG,"Authentication error");
                }
            },"user","secret");
        }
       
        @Override
        public void onClose(int code, String reason) {
            Log.i(TAG,"Connection closed.");
            Log.i(TAG,"Reason: "+reason);
        }
    });
}

+=================== PYTHON SERVER

class MyServerProtocol(WampCraServerProtocol):
“”"
Authenticating WAMP server using WAMP-Challenge-Response-Authentication (“WAMP-CRA”).
“”"
# # our pseudo user/permissions database

# # auth extra sent by server
# #
if True:
    # # when using salted WAMP-CRA, we send salt info ..
    AUTHEXTRA = {'salt': "RANDOM SALT", 'keylen': 32, 'iterations': 1000}
else:
    AUTHEXTRA = None

# # secrets by authkey
# #
SECRETS = {'foobar': WampCraProtocol.deriveKey('secret', AUTHEXTRA)}

print "Auth Extra/Secrets"
print AUTHEXTRA
print SECRETS

# # permissions by authkey
# #
PERMISSIONS = {'foobar': {'pubsub': [{'uri': 'http://example.com/topics/',
                                      'prefix': True,
                                      'pub': True,
                                      'sub': True}],
                          'rpc': [{'uri': 'http://example.com/procedures/hello',
                                   'call': True}]},
               None: {'pubsub': [{'uri': 'http://example.com/topics/mytopic1',
                                  'prefix': False,
                                  'pub': False,
                                  'sub': True}],
                      'rpc': []}}

def onSessionOpen(self):

    # # override global client auth options
    self.clientAuthTimeout = 0
    self.clientAuthAllowAnonymous = True
    print "Session open detected."
    # # call base class method
    WampCraServerProtocol.onSessionOpen(self)


def getAuthPermissions(self, authKey, authExtra):
    # # return permissions which will be granted for the auth key
    # # when the authentication succeeds
    return {'permissions': self.PERMISSIONS.get(authKey, None),
            'authextra': self.AUTHEXTRA}


def getAuthSecret(self, authKey):
    # # return the auth secret for the given auth key or None when the auth key
    # # does not exist
    secret = self.SECRETS.get(authKey, None)
    if False:
        # we may return the secret as a string ..
        return secret
    else:
        # .. or return a Deferred that when fires provides the secret as a string.
        # This can be used i.e. when you retrieve the secret from a (real) database.
        d = defer.Deferred()
        d.callback(secret)
        return d


def onAuthenticated(self, authKey, perms):
    # # fired when authentication succeeds
   
    # # register PubSub topics from the auth permissions
    self.registerForPubSubFromPermissions(perms['permissions'])
   
    # # register RPC endpoints (for now do that manually, keep in sync with perms)
    if authKey is not None:
        self.registerForRpc(self,
                            'http://example.com/procedures/',
                            [MyServerProtocol.hello])


@exportRpc("hello")
def hello(self, name):
    return "Hello back %s!" % name

factory = WebSocketServerFactory("ws://localhost:9000", debug = True)
factory.protocol = MyServerProtocol
listenWS(factory)

reactor.run()
0 Likes

#2

Hi,

in general, please don't use "trunk" .. eg AutobahnPython is currently under major refactoring for WAMPv2.

Use AutobahnPython 0.6.5 (either the tagged version from GitHub or the package from PyPi).

In your server you are using the WebSocketFactory, not the WampFactory .. try this code:

https://github.com/tavendo/AutobahnPython/blob/master/examples/wamp/authentication/server.py#L130

Regarding AutobahnAndroid: I haven't touched the code for long .. you might try trunk in this case with this example:

https://github.com/tavendo/AutobahnAndroid/tree/master/Demo/CRAClient

Hope this helps,

/Tobias

···

Am 17.12.2013 17:27, schrieb jumbo...@gmail.com:

Hi there. I'm trying to use WampCra from android to python. I am
having some issues and I am not sure whether it is due to compatibility
between client and server. I'm running inside a virtualenv on the
python sid

I'm trying to follow the demo here:

https://github.com/tavendo/AutobahnAndroid/blob/master/Demo/CRAClient/src/de/tavendo/autobahn/craclient/CRAClientActivity.java

Note that on line 96 I had to use Wamp.ConnectionHandler() instead of
WampCra.ConnectionHandler() because the latter does not resolve for me.

Originally I am using AutobahnAndroid 0.7.2 (git trunk) and
AutobahnPython 0.6.5 (version from pip). When I try to connect on these
versions I get a connection failure with server error 400 (this server
only speaks wamp).

When I build and install the git truck AutobahnPython in my virtualenv
(using setup.py with virtualenv active *and* using the python binary for
my virtualenv) I get the following import error:

>>> import autobahn
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File
"$HOME/Development/virtualenvs/qserver/local/lib/python2.7/site-packages/autobahn-0.7.0-py2.7.egg/autobahn/__init__.py",
line 38, in <module>
     import wamp2
   File
"$HOME/Development/virtualenvs/qserver/local/lib/python2.7/site-packages/autobahn-0.7.0-py2.7.egg/autobahn/wamp2/__init__.py",
line 27, in <module>
     import websocket
   File
"$HOME/Development/virtualenvs/qserver/local/lib/python2.7/site-packages/autobahn-0.7.0-py2.7.egg/autobahn/wamp2/websocket.py",
line 34, in <module>
     from serializer import WampJsonSerializer, WampMsgPackSerializer
ImportError: cannot import name WampMsgPackSerializer

Here is my code. Please note that I am still just trying to get a
connection. The authentication code might not be exactly right:

+============ ANDROID

(called directly from my onCreate)
private void startAutobahnClient() {
         final String wsuri = "ws://6.6.0.132:9000";
         Log.i(TAG,"Connecting to server");
         mConnection.connect(wsuri, new Wamp.ConnectionHandler() {
             @Override
             public void onOpen() {
                 Log.i(TAG,"Connected to "+wsuri+"\n");
                 mConnection.authenticate(new WampCra.AuthHandler() {

                     @Override
                     public void onAuthSuccess(Object arg0) {
                         Log.i(TAG,"Authenticated!");
                     }

                     @Override
                     public void onAuthError(String arg0, String arg1) {
                         Log.e(TAG,"Authentication error");
                     }
                 },"user","secret");
             }

             @Override
             public void onClose(int code, String reason) {
                 Log.i(TAG,"Connection closed.");
                 Log.i(TAG,"Reason: "+reason);
             }
         });
     }

+=================== PYTHON SERVER

class MyServerProtocol(WampCraServerProtocol):
     """
     Authenticating WAMP server using
WAMP-Challenge-Response-Authentication ("WAMP-CRA").
     """
     # # our pseudo user/permissions database

     # # auth extra sent by server
     # #
     if True:
         # # when using salted WAMP-CRA, we send salt info ..
         AUTHEXTRA = {'salt': "RANDOM SALT", 'keylen': 32, 'iterations':
1000}
     else:
         AUTHEXTRA = None

     # # secrets by authkey
     # #
     SECRETS = {'foobar': WampCraProtocol.deriveKey('secret', AUTHEXTRA)}

     print "Auth Extra/Secrets"
     print AUTHEXTRA
     print SECRETS

     # # permissions by authkey
     # #
     PERMISSIONS = {'foobar': {'pubsub': [{'uri':
'http://example.com/topics/',
                                           'prefix': True,
                                           'pub': True,
                                           'sub': True}],
                               'rpc': [{'uri':
'http://example.com/procedures/hello',
                                        'call': True}]},
                    None: {'pubsub': [{'uri':
'http://example.com/topics/mytopic1',
                                       'prefix': False,
                                       'pub': False,
                                       'sub': True}],
                           'rpc': []}}

     def onSessionOpen(self):

         # # override global client auth options
         self.clientAuthTimeout = 0
         self.clientAuthAllowAnonymous = True
         print "Session open detected."
         # # call base class method
         WampCraServerProtocol.onSessionOpen(self)

     def getAuthPermissions(self, authKey, authExtra):
         # # return permissions which will be granted for the auth key
         # # when the authentication succeeds
         return {'permissions': self.PERMISSIONS.get(authKey, None),
                 'authextra': self.AUTHEXTRA}

     def getAuthSecret(self, authKey):
         # # return the auth secret for the given auth key or None when
the auth key
         # # does not exist
         secret = self.SECRETS.get(authKey, None)
         if False:
             # we may return the secret as a string ..
             return secret
         else:
             # .. or return a Deferred that when fires provides the
secret as a string.
             # This can be used i.e. when you retrieve the secret from a
(real) database.
             d = defer.Deferred()
             d.callback(secret)
             return d

     def onAuthenticated(self, authKey, perms):
         # # fired when authentication succeeds

         # # register PubSub topics from the auth permissions
         self.registerForPubSubFromPermissions(perms['permissions'])

         # # register RPC endpoints (for now do that manually, keep in
sync with perms)
         if authKey is not None:
             self.registerForRpc(self,
                                 'http://example.com/procedures/',
                                 [MyServerProtocol.hello])

     @exportRpc("hello")
     def hello(self, name):
         return "Hello back %s!" % name

     factory = WebSocketServerFactory("ws://localhost:9000", debug = True)
     factory.protocol = MyServerProtocol
     listenWS(factory)

     reactor.run()

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Likes