Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signalr conncet to server with user pass #52

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ signalr-client-py is written and maintained by
and the contributors (ordered by the date of first contribution):
Ekevoo @ ekevoo
Michael Herrmann @ mherrmann
Cyrille B. Delavenne @ cbdelavenne
Gal Ouz @ galoz
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='signalr-client',
version='0.0.6',
version='0.0.7',
description='Simple SignalR client for Python',
long_description=long_description,
url='https://github.com/TargetProcess/signalr-client-py',
Expand Down
3 changes: 2 additions & 1 deletion signalr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from gevent import monkey

monkey.patch_socket()
monkey.patch_ssl()

from ._connection import Connection

__version__ = '0.0.6'
__version__ = '0.0.7'
3 changes: 2 additions & 1 deletion signalr/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Connection:
def __init__(self, url, session):
self.url = url
self.__hubs = {}
self.qs = {}
self.__send_counter = -1
self.token = None
self.data = None
Expand Down Expand Up @@ -50,7 +51,7 @@ def start(self):

def wrapped_listener():
listener()
gevent.sleep(0)
gevent.sleep()

self.__greenlet = gevent.spawn(wrapped_listener)
self.started = True
Expand Down
27 changes: 11 additions & 16 deletions signalr/transports/_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ def _get_name(self):
pass

def negotiate(self):
url = self.__get_base_url(self._connection.url,
self._connection,
url = self.__get_base_url(self._connection,
'negotiate',
connectionData=self._connection.data)
negotiate = self._session.get(url)

negotiate.raise_for_status()

return negotiate.json()

@abstractmethod
Expand All @@ -44,32 +45,26 @@ def accept(self, negotiate_data):
return True

def _handle_notification(self, message):
if len(message) == 0:
return

data = json.loads(message)
self._connection.received.fire(**data)
gevent.sleep(0)
if len(message) > 0:
data = json.loads(message)
self._connection.received.fire(**data)
gevent.sleep()

def _get_url(self, action, **kwargs):
args = kwargs.copy()
args['transport'] = self._get_name()
args['connectionToken'] = self._connection.token
args['connectionData'] = self._connection.data

url = self._get_transport_specific_url(self._connection.url)

return self.__get_base_url(url, self._connection, action, **args)

def _get_transport_specific_url(self, url):
return url
return self.__get_base_url(self._connection, action, **args)

@staticmethod
def __get_base_url(url, connection, action, **kwargs):
def __get_base_url(connection, action, **kwargs):
args = kwargs.copy()
args.update(connection.qs)
args['clientProtocol'] = connection.protocol_version
query = '&'.join(['{key}={value}'.format(key=key, value=quote_plus(args[key])) for key in args])

return '{url}/{action}?{query}'.format(url=url,
return '{url}/{action}?{query}'.format(url=connection.url,
action=action,
query=query)
14 changes: 9 additions & 5 deletions signalr/transports/_ws_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ def __init__(self, session, connection):
def _get_name(self):
return 'webSockets'

def _get_transport_specific_url(self, url):
@staticmethod
def __get_ws_url_from(url):
parsed = urlparse(url)
scheme = 'wss' if parsed.scheme == 'https' else 'ws'
url_data = (scheme, parsed.netloc, parsed.path, parsed.params, parsed.query, parsed.fragment)

return urlunparse(url_data)

def start(self):
self.ws = create_connection(self._get_url('connect'),
ws_url = self.__get_ws_url_from(self._get_url('connect'))

self.ws = create_connection(ws_url,
header=self.__get_headers(),
cookie=self.__get_cookie_str(),
enable_multithread=True)
self._session.get(self._get_url('start'))

def _receive():
for notification in self.ws:
Expand Down Expand Up @@ -65,6 +69,6 @@ def __get_headers(self):

def __get_cookie_str(self):
return '; '.join([
'%s=%s' % (name, value)
for name, value in self._session.cookies.items()
])
'%s=%s' % (name, value)
for name, value in self._session.cookies.items()
])