invisibleroads / socketIO-client

A socket.io client library for Python
http://pypi.python.org/pypi/socketIO-client
MIT License
447 stars 205 forks source link

receive extra character when on listening #165

Open SuperCipher opened 7 years ago

SuperCipher commented 7 years ago

receive extra character when using .on listening method

client side (python)

python 2.7 OSX

import logging
from socketIO_client import SocketIO, BaseNamespace

logging.getLogger('socketIO-client').setLevel(logging.DEBUG)
logging.basicConfig()

def on_test_response(*args):
    print('on_test_response', args)

def on_connect():
    print('connect')

def on_disconnect():
    print('disconnect')

def on_fps_com_response(*args):
    print('on_fps_com_response', args)

class FpsNamespace(BaseNamespace):

    def on_fps_com_response(self, *args):
        print('on_fps_com_response', args)

socketIO = SocketIO('https://localhost', 8080, verify=False)
FpsNamespace = socketIO.define(FpsNamespace, '/fps-namespace')

data = ''

FpsNamespace.emit('fps_com', 'aaa')
FpsNamespace.on('fps_com', on_fps_com_response)

socketIO.wait(seconds=5)

what receive from the client-end (received the unexpected)

expect to receive copy actual recieve (u'copy',)

server side code

"socket.io": "^1.7.3" nodeJs 8.6.0 OSX

const fps_nsp = io.of('/fps-namespace');
fps_nsp.on('connection', (socket) => {
  // socket.emit('fps_com', { hello: 'Hey there browser!' });
  socket.on('fps_com', (data) => {
    console.log(data);
    socket.emit('fps_com', 'copy');
  });
  socket.on('disconnect', () => {
    console.log('Socket disconnected');
  });
});

what recieve from the server-end (recieved as expected) aaa

If I change the code in client side (python) from verify=False to verify='cert.pem' I got an error

ERROR:urllib3.connection:Certificate did not match expected hostname: localhost. Certificate: {'notBefore': u'Aug 29 09:08:30 2017 GMT', 'serialNumber': u'9A6416AB3B2A6919', 'notAfter': 'Aug 29 09:08:30 2018 GMT', 'version': 3L, 'subject': ((('countryName', u'TH'),), (('stateOrProvinceName', u'xxx'),), (('localityName', u'xxx'),), (('organizationName', u'Fps moonshot Ltd'),), (('organizationalUnitName', u'Fps developer'),), (('commonName', u'xxx'),), (('emailAddress', u'email@hotmail.com'),)), 'issuer': ((('countryName', u'TH'),), (('stateOrProvinceName', u'xxx'),), (('localityName', u'xxx'),), (('organizationName', u'Fps moonshot Ltd'),), (('organizationalUnitName', u'Fps developer'),), (('commonName', u'xxx'),), (('emailAddress', u'email@hotmail.com'),))} WARNING:socketIO-client-2:localhost:8080/socket.io [waiting for connection] HTTPSConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /socket.io/?EIO=3&transport=polling&t=1504204491104-0 (Caused by SSLError(CertificateError("hostname 'localhost' doesn't match u'xxx'",),))

Ps. Sorry for being a newbie and Thank you for answering

duvallj commented 6 years ago

You are actually not receiving any extra characters; the response is fine. What you are seeing is a singleton tuple with a unicode string inside, what the client is packaging the string 'copy' as. To get the string you want, simply do str(args[0]) instead of args in your callback.