allow to pass in a different resource than just 'socket.io'

This commit is contained in:
Patrick Huber 2014-07-28 09:06:51 +02:00
commit 9285f3da02
2 changed files with 13 additions and 9 deletions

View file

@ -122,6 +122,7 @@ Specify params, headers, cookies, proxies thanks to the `requests`_ library. ::
from base64 import b64encode from base64 import b64encode
SocketIO('localhost', 8000, SocketIO('localhost', 8000,
resource='my.io',
params={'q': 'qqq'}, params={'q': 'qqq'},
headers={'Authorization': 'Basic ' + b64encode('username:password')}, headers={'Authorization': 'Basic ' + b64encode('username:password')},
cookies={'a': 'aaa'}, cookies={'a': 'aaa'},
@ -175,8 +176,8 @@ Credits
.. _Lucas Klein: https://github.com/lukashed .. _Lucas Klein: https://github.com/lukashed
.. _Rui Chicoria: https://github.com/rchicoria .. _Rui Chicoria: https://github.com/rchicoria
.. _Travis Odom: https://github.com/burstaholic .. _Travis Odom: https://github.com/burstaholic
.. _Eric Chen: https://github.com/taiyangc .. _Eric Chen: https://github.com/taiyangc
.. _Denis Zinevich: https://github.com/dzinevich .. _Denis Zinevich: https://github.com/dzinevich
.. _Thiago Hersan: https://github.com/thiagohersan .. _Thiago Hersan: https://github.com/thiagohersan
.. _Nayef Copty: https://github.com/nayefc .. _Nayef Copty: https://github.com/nayefc
.. _Patrick Huber: https://github.com/stackmagic

View file

@ -20,6 +20,7 @@ RETRY_INTERVAL_IN_SECONDS = 1
class BaseNamespace(object): class BaseNamespace(object):
'Define client behavior' 'Define client behavior'
def __init__(self, _transport, path): def __init__(self, _transport, path):
@ -109,6 +110,7 @@ class BaseNamespace(object):
class SocketIO(object): class SocketIO(object):
"""Create a socket.io client that connects to a socket.io server """Create a socket.io client that connects to a socket.io server
at the specified host and port. at the specified host and port.
@ -119,6 +121,7 @@ class SocketIO(object):
- Pass query params, headers, cookies, proxies as keyword arguments. - Pass query params, headers, cookies, proxies as keyword arguments.
SocketIO('localhost', 8000, SocketIO('localhost', 8000,
resource='my.io',
params={'q': 'qqq'}, params={'q': 'qqq'},
headers={'Authorization': 'Basic ' + b64encode('username:password')}, headers={'Authorization': 'Basic ' + b64encode('username:password')},
cookies={'a': 'aaa'}, cookies={'a': 'aaa'},
@ -127,8 +130,8 @@ class SocketIO(object):
def __init__( def __init__(
self, host, port=None, Namespace=BaseNamespace, self, host, port=None, Namespace=BaseNamespace,
wait_for_connection=True, transports=TRANSPORTS, **kw): wait_for_connection=True, transports=TRANSPORTS, resource='socket.io', **kw):
self.is_secure, self.base_url = _parse_host(host, port) self.is_secure, self.base_url = _parse_host(host, port, resource)
self.wait_for_connection = wait_for_connection self.wait_for_connection = wait_for_connection
self._namespace_by_path = {} self._namespace_by_path = {}
self.client_supported_transports = transports self.client_supported_transports = transports
@ -361,14 +364,14 @@ def find_callback(args, kw=None):
return None, args return None, args
def _parse_host(host, port): def _parse_host(host, port, resource):
if not host.startswith('http'): if not host.startswith('http'):
host = 'http://' + host host = 'http://' + host
url_pack = urlparse(host) url_pack = urlparse(host)
is_secure = url_pack.scheme == 'https' is_secure = url_pack.scheme == 'https'
port = port or url_pack.port or (443 if is_secure else 80) port = port or url_pack.port or (443 if is_secure else 80)
base_url = '%s:%d%s/socket.io/%s' % ( base_url = '%s:%d%s/%s/%s' % (
url_pack.hostname, port, url_pack.path, PROTOCOL_VERSION) url_pack.hostname, port, url_pack.path, resource, PROTOCOL_VERSION)
return is_secure, base_url return is_secure, base_url