def testParseUrl(self): p = ws._parse_url("ws://www.example.com/r") self.assertEquals(p[0], "www.example.com") self.assertEquals(p[1], 80) self.assertEquals(p[2], "/r") self.assertEquals(p[3], False) p = ws._parse_url("ws://www.example.com/r/") self.assertEquals(p[0], "www.example.com") self.assertEquals(p[1], 80) self.assertEquals(p[2], "/r/") self.assertEquals(p[3], False) p = ws._parse_url("ws://www.example.com/") self.assertEquals(p[0], "www.example.com") self.assertEquals(p[1], 80) self.assertEquals(p[2], "/") self.assertEquals(p[3], False) p = ws._parse_url("ws://www.example.com") self.assertEquals(p[0], "www.example.com") self.assertEquals(p[1], 80) self.assertEquals(p[2], "/") self.assertEquals(p[3], False) p = ws._parse_url("ws://www.example.com:8080/r") self.assertEquals(p[0], "www.example.com") self.assertEquals(p[1], 8080) self.assertEquals(p[2], "/r") self.assertEquals(p[3], False) p = ws._parse_url("ws://www.example.com:8080/") self.assertEquals(p[0], "www.example.com") self.assertEquals(p[1], 8080) self.assertEquals(p[2], "/") self.assertEquals(p[3], False) p = ws._parse_url("ws://www.example.com:8080") self.assertEquals(p[0], "www.example.com") self.assertEquals(p[1], 8080) self.assertEquals(p[2], "/") self.assertEquals(p[3], False) p = ws._parse_url("wss://www.example.com:8080/r") self.assertEquals(p[0], "www.example.com") self.assertEquals(p[1], 8080) self.assertEquals(p[2], "/r") self.assertEquals(p[3], True) p = ws._parse_url("wss://www.example.com:8080/r?key=value") self.assertEquals(p[0], "www.example.com") self.assertEquals(p[1], 8080) self.assertEquals(p[2], "/r?key=value") self.assertEquals(p[3], True) self.assertRaises(ValueError, ws._parse_url, "http://www.example.com/r")
def connect(self, url, **options): """connect to socketio and then upgrade to websocket transport. Example:""" def read_block(sock): """read from the socket until empty line, return list of lines""" lines = [] line = "" while True: res = sock.recv(1) line += res if res == "": return None if res == "\n": line = line.strip() if line == "": return lines lines.append(line) line = "" # pylint: disable=W0212 hostname, port, resource, is_secure = websocket._parse_url(url) self.sock.connect((hostname, port)) if is_secure: self.io_sock = websocket._SSLSocketWrapper(self.sock) path_a = resource if "query" in options: path_a += "?" + options["query"] self.io_sock.send("GET %s HTTP/1.1\r\n" % path_a) self.io_sock.send("Host: %s:%d\r\n" % (hostname, port)) self.io_sock.send("User-Agent: genBTC-bot\r\n") self.io_sock.send("Accept: text/plain\r\n") self.io_sock.send("Connection: keep-alive\r\n") self.io_sock.send("\r\n") headers = read_block(self.io_sock) if not headers: raise IOError("disconnected while reading headers") if not "200" in headers[0]: raise IOError("wrong answer: %s" % headers[0]) result = read_block(self.io_sock) if not result: raise IOError("disconnected while reading socketio session ID") if len(result) != 3: raise IOError("invalid response from socket.io server") ws_id = result[1].split(":")[0] resource += "/websocket/" + ws_id if "query" in options: resource += "?" + options["query"] self._handshake(hostname, port, resource, **options)
def testParseUrl(self): p = ws._parse_url("ws://www.example.com/r") self.assertEqual(p[0], "www.example.com") self.assertEqual(p[1], 80) self.assertEqual(p[2], "/r") self.assertEqual(p[3], False) p = ws._parse_url("ws://www.example.com/r/") self.assertEqual(p[0], "www.example.com") self.assertEqual(p[1], 80) self.assertEqual(p[2], "/r/") self.assertEqual(p[3], False) p = ws._parse_url("ws://www.example.com/") self.assertEqual(p[0], "www.example.com") self.assertEqual(p[1], 80) self.assertEqual(p[2], "/") self.assertEqual(p[3], False) p = ws._parse_url("ws://www.example.com") self.assertEqual(p[0], "www.example.com") self.assertEqual(p[1], 80) self.assertEqual(p[2], "/") self.assertEqual(p[3], False) p = ws._parse_url("ws://www.example.com:8080/r") self.assertEqual(p[0], "www.example.com") self.assertEqual(p[1], 8080) self.assertEqual(p[2], "/r") self.assertEqual(p[3], False) p = ws._parse_url("ws://www.example.com:8080/") self.assertEqual(p[0], "www.example.com") self.assertEqual(p[1], 8080) self.assertEqual(p[2], "/") self.assertEqual(p[3], False) p = ws._parse_url("ws://www.example.com:8080") self.assertEqual(p[0], "www.example.com") self.assertEqual(p[1], 8080) self.assertEqual(p[2], "/") self.assertEqual(p[3], False) p = ws._parse_url("wss://www.example.com:8080/r") self.assertEqual(p[0], "www.example.com") self.assertEqual(p[1], 8080) self.assertEqual(p[2], "/r") self.assertEqual(p[3], True) p = ws._parse_url("wss://www.example.com:8080/r?key=value") self.assertEqual(p[0], "www.example.com") self.assertEqual(p[1], 8080) self.assertEqual(p[2], "/r?key=value") self.assertEqual(p[3], True) self.assertRaises(ValueError, ws._parse_url, "http://www.example.com/r") if sys.version_info[0] == 2 and sys.version_info[1] < 7: return p = ws._parse_url("ws://[2a03:4000:123:83::3]/r") self.assertEqual(p[0], "2a03:4000:123:83::3") self.assertEqual(p[1], 80) self.assertEqual(p[2], "/r") self.assertEqual(p[3], False) p = ws._parse_url("ws://[2a03:4000:123:83::3]:8080/r") self.assertEqual(p[0], "2a03:4000:123:83::3") self.assertEqual(p[1], 8080) self.assertEqual(p[2], "/r") self.assertEqual(p[3], False) p = ws._parse_url("wss://[2a03:4000:123:83::3]/r") self.assertEqual(p[0], "2a03:4000:123:83::3") self.assertEqual(p[1], 443) self.assertEqual(p[2], "/r") self.assertEqual(p[3], True) p = ws._parse_url("wss://[2a03:4000:123:83::3]:8080/r") self.assertEqual(p[0], "2a03:4000:123:83::3") self.assertEqual(p[1], 8080) self.assertEqual(p[2], "/r") self.assertEqual(p[3], True)