Beispiel #1
0
def test_protocol_header_is_optional(websocket):
    request_headers = websocket.build_request()
    assert b'Sec-WebSocket-Protocol' not in request_headers

    websocket_with_protocols = WebSocket('ws://example.com/',
                                         protocols=('proto1', 'proto2'))
    request_headers = websocket_with_protocols.build_request()
    assert b'Sec-WebSocket-Protocol: proto1, proto2' in request_headers
Beispiel #2
0
 def setup(self):
     if self.is_setup:
         print('setup is done earlier, return')
         return
     command = 'roslaunch rosbridge_server rosbridge_websocket.launch'
     self.bridge_proc_id = external_command_pool.start_command(command)
     self.ws = WebSocket('ws://localhost:9090')
     self.monitor_t = Thread(target=self.monitor)
     self.monitor_t.start()
     self.is_setup = True
def websocket_with_fake_session(monkeypatch):
    monkeypatch.setattr(
        'os.urandom', lambda len: b'\x00' * len)

    ws = WebSocket('ws://example.com')
    ws.state.session = FakeSession()
    return ws
def test_simple_run_with_close(monkeypatch, mocker):
    """Test graceful close."""
    monkeypatch.setattr('os.urandom', b'\x00'.__mul__)
    Mocket.register(
        MocketEntry(('example.com', 80),
                    [(b'HTTP/1.1 101 Switching Protocols\r\n'
                      b'Upgrade: websocket\r\n'
                      b'Connection: Upgrade\r\n'
                      b'Sec-WebSocket-Accept: icx+yqv66kxgm0fcwalwlflwtai=\r\n'
                      b'\r\n'
                      b'\x81\x81\x00\x00\x00\x00A\x88\x80\xba51e')]))

    session = WebsocketSession(WebSocket('ws://example.com/'))
    session._selector_cls = FakeSelector
    session._on_ready()

    session._regular_orig = session._regular

    mocker.patch('lomond.websocket.WebSocket._send_close')
    mocker.patch.object(session.websocket, 'send_ping')
    session.websocket.state.session = session

    _events = list(session.run())

    assert len(_events) == 7
    assert isinstance(_events[0], events.Connecting)
    assert isinstance(_events[1], events.Connected)
    assert isinstance(_events[2], events.Ready)
    assert isinstance(_events[3], events.Poll)
    assert isinstance(_events[4], events.Text)
    assert isinstance(_events[5], events.Closing)
    assert isinstance(_events[6], events.Disconnected)
    assert _events[6].graceful
def test_context_manager():
    ws = WebSocket('ws://example.com/')
    session = WebsocketSession(ws)
    session._selector_cls = FakeSelector
    session._on_ready()
    with ws:
        for event in ws:
            pass
Beispiel #6
0
        def go():
            sub_params = {'type': 'subscribe', 'product_ids': self.products}
            ws = WebSocket(gdax_ws_endpoint)

            for event in ws.connect():
                if event.name == 'poll':
                    ws.send_text(json.dumps(sub_params))
                elif event.name == 'text':

                    # print(event.text)

                    try:
                        msg = json.loads(event.text)
                        print(msg['price'])
                        print(msg['type'])
                    except:
                        print('couldnt parse msg')
def test_unresponsive(monkeypatch, mocker):
    """Check ping timeout."""
    monkeypatch.setattr(
        'os.urandom', b'\x00'.__mul__
    )
    Mocket.register(
        MocketEntry(
            ('example.com', 80),
            [(
                b'HTTP/1.1 101 Switching Protocols\r\n'
                b'Upgrade: websocket\r\n'
                b'Connection: Upgrade\r\n'
                b'Sec-WebSocket-Accept: icx+yqv66kxgm0fcwalwlflwtai=\r\n'
                b'\r\n'
                b'\x81\x81\x00\x00\x00\x00A'
            )]
        )
    )

    # mocket doesn't support .pending() call which is used when ssl is used
    session = WebsocketSession(WebSocket('ws://example.com/'))
    session._selector_cls = FakeSelector
    session._on_ready()
    # well, we have to cheat a little. The thing is, inner loop of
    # run() sets last poll time to time.time and so we would have to
    # wait for some time to actually hit poll / ping. This is not desirable
    # so we can do the following:
    # save original _regular call into _regular_orig
    # (_regular is a first - well, technically, a second) call inside run
    # after _poll_start is set which makes it a nice candidate for monkey-patch
    # location. Here's how we do it:
    session._regular_orig = session._regular

    mocker.patch(
        'lomond.websocket.WebSocket._send_close')
    mocker.patch.object(session.websocket, 'send_ping')

    _events = []
    iter_events = iter(session.run(ping_timeout=5))

    for event in iter_events:
        _events.append(event)
        if event.name == 'text':
            break

    with freeze_time("1994-05-01 18:41:00"):
        for event in iter_events:
            _events.append(event)

    assert len(_events) == 8
    assert isinstance(_events[0], events.Connecting)
    assert isinstance(_events[1], events.Connected)
    assert isinstance(_events[2], events.Ready)
    assert isinstance(_events[3], events.Poll)
    assert isinstance(_events[4], events.Text)
    assert isinstance(_events[5], events.Poll)
    assert isinstance(_events[6], events.Unresponsive)
    assert isinstance(_events[7], events.Disconnected)
def session(monkeypatch):
    monkeypatch.setattr('os.urandom', b'\xaa'.__mul__)
    # ^^ the above line will be significant in the test where we want
    # to validate the headers being sent to the socket. Namely, the
    # websocket key which is based on os.urandom. Obviously, we can't
    # have an actual random call here because the test wouldn't be
    # deterministic, hence this sequence of bytes.

    return WebsocketSession(WebSocket('wss://example.com/'))
Beispiel #9
0
 def _prep_connection(self):
     ws = WebSocket(self.endpoint, compress=True)
     apikey = self.api_key
     secret = self.secret
     nonce = generate_nonce()
     ws.add_header(str.encode("api-nonce"), str.encode(str(nonce)))
     ws.add_header(
         str.encode("api-signature"),
         str.encode(
             str(
                 generate_signature_bitmex(secret, 'GET', '/realtime',
                                           nonce, ''))))
     ws.add_header(str.encode("api-key"), str.encode(str(apikey)))
     return ws
Beispiel #10
0
def test_simple_run(monkeypatch, mocker):
    monkeypatch.setattr('os.urandom', b'\x00'.__mul__)
    Mocket.register(
        MocketEntry(('example.com', 80),
                    [(b'HTTP/1.1 101 Switching Protocols\r\n'
                      b'Upgrade: websocket\r\n'
                      b'Connection: Upgrade\r\n'
                      b'Sec-WebSocket-Accept: icx+yqv66kxgm0fcwalwlflwtai=\r\n'
                      b'\r\n'
                      b'\x81\x81\x00\x00\x00\x00A')]))

    # mocket doesn't support .pending() call which is used when ssl is used
    session = WebsocketSession(WebSocket('ws://example.com/'))
    session._on_ready()
    # well, we have to cheat a little. The thing is, inner loop of
    # run() sets last poll time to time.time and so we would have to
    # wait for some time to actually hit poll / ping. This is not desirable
    # so we can do the following:
    # save original _regular call into _regular_orig
    # (_regular is a first - well, technically, a second) call inside run
    # after _poll_start is set which makes it a nice candidate for monkey-patch
    # location. Here's how we do it:
    session._regular_orig = session._regular

    mocker.patch('lomond.websocket.WebSocket._send_close')
    mocker.patch.object(session.websocket, 'send_ping')
    mocker.patch('lomond.session.WebsocketSession._select',
                 lambda self, sock, poll: [True, False])

    _events = list(session.run())

    assert len(_events) == 6
    assert isinstance(_events[0], events.Connecting)
    assert isinstance(_events[1], events.Connected)
    assert isinstance(_events[2], events.Ready)
    assert isinstance(_events[3], events.Poll)
    assert isinstance(_events[4], events.Text)
    assert isinstance(_events[5], events.Disconnected)
Beispiel #11
0
class client():
    def __init__(self, connect=True, proxies=None):

        self.id = ""
        self.owner = ""
        self.ws = None
        self.eventID = 1

        self.reconnect_url = ""

        try:
            fp = open("id.cfg", "r")
            self.id = fp.readline()
            self.owner = fp.readline()
            print("id:" + self.id)
            print("owner:" + self.owner)
            fp.close()
        except IOError:
            print("unable to read id.cfg")
            quit()

        try:
            fp = open("token.dat", "rb")
            self.token = fp.readline()
            fp.close()
        except IOError:
            print("unable to read token.dat")
            quit()

        try:
            fp = open("acknowledged.dat", "rb")
            self.acknowledged = pickle.load(fp)
            fp.close()
        except IOError:
            self.acknowledged = []

    def webApiSend(self, contentType, apiMethod, dat=None):

        if dat != None:
            if contentType == "application/json":
                dat = json.dumps(dat).encode('utf8')
            else:
                dat = urllib.parse.urlencode(dat).encode("utf-8")

        header = {"Content-type": contentType, "Authorization": self.token}

        req = urllib.request.Request(url='https://slack.com/api/' + apiMethod,
                                     headers=header,
                                     method='POST',
                                     data=dat)
        res = urllib.request.urlopen(req, timeout=5)

        return res.read().decode('utf-8')

    def getRTM(self):
        return self.webApiSend("application/x-www-form-urlencoded",
                               "rtm.connect")

    def acknowledge(self, channel, user):
        self.sendMeMessage(channel, "acknowledges your presence.")

        if user in self.acknowledged:
            return

        self.acknowledged.append(user)
        fp = open("acknowledged.dat", "wb")
        pickle.dump(
            self.acknowledged,
            fp)  #to save to file and load on startup to make it persistant
        fp.close()

    def connect(self):
        rtm = json.loads(self.getRTM())

        self.id = rtm["self"]["id"]
        self.ws = WebSocket(rtm["url"])

    def sendMeMessage(self, channel, msg):
        dat = {"channel": channel, "text": msg}
        self.webApiSend("application/x-www-form-urlencoded", "chat.meMessage",
                        dat)

    def sendMessage(self, channel, msg):
        send = '{"id": ' + str(
            self.eventID
        ) + ', "type": "message", "channel": "' + channel + '", "text": "' + msg + '"}'
        self.ws.send_text(send)
        self.eventID = self.eventID + 1

    def messageHandler(self, channel, msg, elevated):

        if not elevated:
            if (msg == "banana"):
                self.sendMessage(
                    channel,
                    "mew mew mew mew mew mew mew. Thats you. Thats what you sound like. deplorable."
                )

            #elevated permissions needed for anything below this point
            return

        if (msg == "banana"):
            self.sendMessage(channel, "orange ya glad I didnt say banana?")

        if (msg == "laputan machine"):
            self.sendMessage(channel, "judas.")
            quit()

    def mainLoop(self):

        for event in self.ws:
            if event.name == "text":
                msg = json.loads(event.text)

                print(msg)

                if "type" in msg:
                    if "channel" in msg:
                        channel = msg["channel"]
                        print("")
                        print(channel)
                        print("")

                    #going to actually need to use this at some point
                    if msg["type"] == "reconnect_url":
                        self.reconnect_url = msg["url"]

                    if msg["type"] == "team_join":
                        self.sendMessage(channel,
                                         "You There. Kneel Before Zod.")

                    if msg["type"] == "message":
                        if "subtype" in msg:
                            if msg["subtype"] == "me_message":
                                if msg["text"] == "kneels":
                                    self.acknowledge(channel, msg["user"])

                    #only acknowledged users past this point
                        if msg["user"] not in self.acknowledged:
                            continue

                        #@ message to zod
                        if msg["text"][:12] == "<@" + self.id + ">":
                            self.messageHandler(channel, msg["text"][13:],
                                                msg["user"] == self.owner)
Beispiel #12
0
    login = r.json()
    assert "s" in login
except:
    raise RuntimeError("Could not understand server response to login attempt. Aborting.")
if login['s'] == 0:
    raise RuntimeError("Authentication failed: {}".format(login['m']))
print("Login successful")

#Extract data from websockets for later processing
cdict = requests.utils.dict_from_cookiejar(s.cookies)
cookies = list()
for k in cdict:
    cookies.append('='.join([k,cdict[k]]))
cstr = ';'.join(cookies)

ws = WebSocket('wss://avabur.com/websocket')
ws.add_header('cookie'.encode('utf-8'), cstr.encode('utf-8'))

msgs = dict()
msgs['clan_profile'] = json.dumps({'start': 0, 'vm': 1, 'type': 'page', 'page': 'clans'})
msgs['clan_members'] = json.dumps({'clan': '%%', 'type': 'page', 'page': 'clan_members'})
msgs['clan_treasury'] = json.dumps({'clan': '%%', 'type': 'page', 'page': 'clan_treasury'})
msgs['clan_donations'] = json.dumps({'type': 'page', 'page': 'clan_donations'})
msgs['profile'] = json.dumps({'type': 'page', 'page': 'profile', 'username': '******'})
msgs['allclans'] = json.dumps({"start":0,"type":"page","page":"clans"})

clan = None
treas = None
members = None
donations = None
profiles = dict()
Beispiel #13
0
def test_is_secure(websocket):
    assert websocket.is_secure is False
    assert WebSocket('wss://example.com').is_secure is True
Beispiel #14
0
def test_port_has_to_be_numeric():
    with pytest.raises(ValueError) as e:
        WebSocket('ws://example.com:abc')

    assert str(e.value) == 'illegal port value'
Beispiel #15
0
def test_init_with_query():
    ws = WebSocket('ws://example.com/resource?query')
    assert ws.resource == '/resource?query'
Beispiel #16
0
def websocket(monkeypatch):
    monkeypatch.setattr('os.urandom', lambda len: b'\x00' * len)

    ws = WebSocket('ws://example.com')
    return ws
Beispiel #17
0
    def connect(self):
        rtm = json.loads(self.getRTM())

        self.id = rtm["self"]["id"]
        self.ws = WebSocket(rtm["url"])
from lomond.websocket import WebSocket
import json
import requests

gdax_http_endpoint = 'https://api.gdax.com'
gdax_ws_endpoint = 'wss://ws-feed.gdax.com'

subscribe_msg = {"type": "subscribe", "product_ids": ["LTC-USD"]}

# ob_request='/products/BTC-USD/book?level=3'


def getProductOrderBook(self, json=None, level=3, product=''):
    r = requests.get(self.url + '/products/%s/book?level=%s' %
                     (product, str(level)))
    return r.json()


ws = WebSocket(gdax_ws_endpoint)
for event in ws.connect():
    # print(event)
    if event.name == 'poll':
        ws.send_text(json.dumps(subscribe_msg))
    elif event.name == 'text':
        print(event.text)
Beispiel #19
0
    login = r.json()
    assert "s" in login
except:
    raise RuntimeError("Could not understand server response to login attempt. Aborting.")
if login['s'] == 0:
    raise RuntimeError("Authentication failed: {}".format(login['m']))
print("Login successful")

#Extract data from websockets for later processing
cdict = requests.utils.dict_from_cookiejar(s.cookies)
cookies = list()
for k in cdict:
    cookies.append('='.join([k,cdict[k]]))
cstr = ';'.join(cookies)

ws = WebSocket('wss://avabur.com/websocket')
ws.add_header('cookie'.encode('utf-8'), cstr.encode('utf-8'))

msgs = dict()
msgs['clan_profile'] = json.dumps({'name': '%%', 'type': 'page', 'page': 'clan_view'})
msgs['clan_members'] = json.dumps({'clan': '%%', 'type': 'page', 'page': 'clan_members'})
msgs['profile'] = json.dumps({'type': 'page', 'page': 'profile', 'username': '******'})

clans = list()
profiles = dict()
listsRequested = 0
entriesExpected = 0

battles = 0
for event in ws:
    #print(event)
def test_send_json_invalid():
    ws = WebSocket('ws://example.com/resource?query')
    with pytest.raises(ValueError):
        ws.send_json('foo', baz='egg')
Beispiel #21
0
class RosbridgeSuitComponent(BaseComponent):
    def __init__(self):
        super(RosbridgeSuitComponent, self).__init__()
        self.is_setup = False
        self.connected = False
        self.running = True
        self.pub_callbacks = {}
        self.srv_callbacks = {}
        self.thread_stopped = False
        self.q = queue.Queue()

    def setup(self):
        if self.is_setup:
            print('setup is done earlier, return')
            return
        command = 'roslaunch rosbridge_server rosbridge_websocket.launch'
        self.bridge_proc_id = external_command_pool.start_command(command)
        self.ws = WebSocket('ws://localhost:9090')
        self.monitor_t = Thread(target=self.monitor)
        self.monitor_t.start()
        self.is_setup = True

    def monitor(self):
        for event in persist(self.ws):
            try:
                if not self.running:
                    self.thread_stopped = True
                    return
                if event.name == 'connecting':
                    print('connecting...')
                elif event.name == 'connected':
                    print('connected')
                    self.connected = True
                elif event.name == 'pong':
                    print('received pong')
                elif event.name == 'poll':
                    print('received poll')
                elif event.name == 'text':
                    json_str = event.text
                    data = json.loads(json_str)
                    if data['op'] == 'publish':
                        topic_id = data['topic']
                        if topic_id in self.pub_callbacks:
                            self.pub_callbacks[topic_id](data['msg'])
                    elif data['op'] == 'service_response':
                        service_id = data['service']
                        if service_id in self.srv_callbacks:
                            self.srv_callbacks[service_id](data['result'])
                    else:
                        print('unknown data: ', data)
                else:
                    print('unhandled event: ', event.name)
                    print(event)
            except:
                logging.exception('error handling %r', event)

    def shutdown(self):
        self.running = False
        while not self.thread_stopped:
            time.sleep(1)
        external_command_pool.end_command(self.bridge_proc_id)

    def send_req(self, req):
        while not self.connected:
            time.sleep(1)
        self.ws.send_text(json.dumps(req))

    def subscribe(self, topic, type, callback):
        req = {'op': 'subscribe', 'topic': topic, 'type': type}
        self.pub_callbacks[topic] = callback
        self.send_req(req)

    def unsubscribe(self, topic):
        req = {'op': 'unsubscribe', 'topic': topic}
        del self.pub_callbacks[topic]
        self.send_req(req)

    def publish(self, topic, msg):
        req = {'op': 'publish', 'topic': topic, 'msg': msg}
        self.send_req(req)

    def call_service(self, service, callback, args=[]):
        req = {'op': 'call_service', 'service': service, 'args': args}
        self.srv_callbacks[service] = callback
        self.send_req(req)

    def respond_service(self, service, result):
        req = {'op': 'service_response', 'service': service, 'result': result}
        self.send_req(req)

    def advertise(self, topic, type):
        req = {'op': 'advertise', 'topic': topic, 'type': type}
        self.send_req(req)

    def advertise_service(self, type, service):
        req = {'op': 'advertise_service', 'type': type, 'service': service}
        self.send_req(req)

    def unadvertise(self, topic):
        req = {'op': 'unadvertise', 'topic': topic}
        self.send_req(req)

    def unadvertise_service(self, service):
        req = {'op': 'unadvertise_service', 'service': service}
        self.send_req(req)
Beispiel #22
0
    assert "s" in login
except:
    raise RuntimeError(
        "Could not understand server response to login attempt. Aborting.")
if login['s'] == 0:
    raise RuntimeError("Authentication failed: {}".format(login['m']))
print("Login successful")

#Extract data from websockets for later processing
cdict = requests.utils.dict_from_cookiejar(s.cookies)
cookies = list()
for k in cdict:
    cookies.append('='.join([k, cdict[k]]))
cstr = ';'.join(cookies)

ws = WebSocket('wss://avabur.com/websocket')
ws.add_header('cookie'.encode('utf-8'), cstr.encode('utf-8'))

msgs = dict()
msgs['crystals'] = json.dumps({
    "type": "page",
    "page": "market",
    "market_type": "currency",
    "page_num": "%%",
    "q": None,
    "ll": None,
    "hl": None,
    "st": "premium"
})
msgs['platinum'] = json.dumps({
    "type": "page",