本文整理汇总了Python中websocket.WebSocket方法的典型用法代码示例。如果您正苦于以下问题:Python websocket.WebSocket方法的具体用法?Python websocket.WebSocket怎么用?Python websocket.WebSocket使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类websocket
的用法示例。
在下文中一共展示了websocket.WebSocket方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_websocket
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def test_websocket(app: Flask, aio: AioHTTP):
"""Test for websocket"""
@app.route('/echo')
@websocket
def echo():
while True:
msg = yield from aio.ws.receive_msg()
if msg.tp == aiohttp.MsgType.text:
aio.ws.send_str(msg.data)
elif msg.tp == aiohttp.MsgType.close:
break
elif msg.tp == aiohttp.MsgType.error:
break
with Server(app, aio) as server:
ws = WebSocket()
ws.connect(server.ws_url('/echo'))
try:
ws.send('foo')
assert 'foo' == ws.recv()
finally:
ws.close()
示例2: testInternalRecvStrict
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def testInternalRecvStrict(self):
sock = ws.WebSocket()
s = sock.sock = SockMock()
s.add_packet(six.b("foo"))
s.add_packet(socket.timeout())
s.add_packet(six.b("bar"))
# s.add_packet(SSLError("The read operation timed out"))
s.add_packet(six.b("baz"))
with self.assertRaises(ws.WebSocketTimeoutException):
sock.frame_buffer.recv_strict(9)
# if six.PY2:
# with self.assertRaises(ws.WebSocketTimeoutException):
# data = sock._recv_strict(9)
# else:
# with self.assertRaises(SSLError):
# data = sock._recv_strict(9)
data = sock.frame_buffer.recv_strict(9)
self.assertEqual(data, six.b("foobarbaz"))
with self.assertRaises(ws.WebSocketConnectionClosedException):
sock.frame_buffer.recv_strict(1)
示例3: testRecvTimeout
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def testRecvTimeout(self):
sock = ws.WebSocket()
s = sock.sock = SockMock()
s.add_packet(six.b("\x81"))
s.add_packet(socket.timeout())
s.add_packet(six.b("\x8dabcd\x29\x07\x0f\x08\x0e"))
s.add_packet(socket.timeout())
s.add_packet(six.b("\x4e\x43\x33\x0e\x10\x0f\x00\x40"))
with self.assertRaises(ws.WebSocketTimeoutException):
sock.recv()
with self.assertRaises(ws.WebSocketTimeoutException):
sock.recv()
data = sock.recv()
self.assertEqual(data, "Hello, World!")
with self.assertRaises(ws.WebSocketConnectionClosedException):
sock.recv()
示例4: testRecvWithProlongedFragmentation
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def testRecvWithProlongedFragmentation(self):
sock = ws.WebSocket()
s = sock.sock = SockMock()
# OPCODE=TEXT, FIN=0, MSG="Once more unto the breach, "
s.add_packet(six.b("\x01\x9babcd.\x0c\x00\x01A\x0f\x0c\x16\x04B\x16\n\x15"
"\rC\x10\t\x07C\x06\x13\x07\x02\x07\tNC"))
# OPCODE=CONT, FIN=0, MSG="dear friends, "
s.add_packet(six.b("\x00\x8eabcd\x05\x07\x02\x16A\x04\x11\r\x04\x0c\x07"
"\x17MB"))
# OPCODE=CONT, FIN=1, MSG="once more"
s.add_packet(six.b("\x80\x89abcd\x0e\x0c\x00\x01A\x0f\x0c\x16\x04"))
data = sock.recv()
self.assertEqual(
data,
"Once more unto the breach, dear friends, once more")
with self.assertRaises(ws.WebSocketConnectionClosedException):
sock.recv()
示例5: testRecvWithFragmentationAndControlFrame
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def testRecvWithFragmentationAndControlFrame(self):
sock = ws.WebSocket()
sock.set_mask_key(create_mask_key)
s = sock.sock = SockMock()
# OPCODE=TEXT, FIN=0, MSG="Too much "
s.add_packet(six.b("\x01\x89abcd5\r\x0cD\x0c\x17\x00\x0cA"))
# OPCODE=PING, FIN=1, MSG="Please PONG this"
s.add_packet(six.b("\x89\x90abcd1\x0e\x06\x05\x12\x07C4.,$D\x15\n\n\x17"))
# OPCODE=CONT, FIN=1, MSG="of a good thing"
s.add_packet(six.b("\x80\x8fabcd\x0e\x04C\x05A\x05\x0c\x0b\x05B\x17\x0c"
"\x08\x0c\x04"))
data = sock.recv()
self.assertEqual(data, "Too much of a good thing")
with self.assertRaises(ws.WebSocketConnectionClosedException):
sock.recv()
self.assertEqual(
s.sent[0],
six.b("\x8a\x90abcd1\x0e\x06\x05\x12\x07C4.,$D\x15\n\n\x17"))
示例6: __init__
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def __init__(self, url):
self.prefixes = {}
self.url = url
self.prefixes_index = {
"4": [],
"6": [],
}
self.hijacks = {}
self.callbacks = {
"hijack": [],
"withdrawal": [],
"announcement": [],
"difference": [],
"error": []
}
ws = websocket.WebSocket()
self.ws = ws
self._connect()
def ping():
ws.send('2')
Timer(5, ping).start()
ping()
示例7: connect
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def connect(self, host=None, port=None):
"""
Connect to the websocket server
:return: Nothing
"""
if host is not None:
self.host = host
if port is not None:
self.port = port
try:
self.ws = websocket.WebSocket()
LOG.info("Connecting...")
self.ws.connect("ws://{}:{}".format(self.host, self.port))
LOG.info("Connected!")
self._auth(self.password)
self._run_threads()
except socket.error as e:
raise exceptions.ConnectionFailure(str(e))
示例8: test_ws_extract_update_with_stats
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def test_ws_extract_update_with_stats(self):
ex_ws = websocket.WebSocket()
ex_ws.connect('ws://localhost:{0}/_client_ws?user={user}&coll=temp&rec=ex&type=extract&url=http://geocities.com/'.format(self.app_port, user=self.anon_user),
header=['Cookie: __test_sesh=' + self.sesh.cookies['__test_sesh']])
def assert_size():
msg = json.loads(ex_ws.recv())
assert msg['size'] > 0
assert msg['pending_size'] == 0
assert msg['stats'] == {'ia': 1}
assert msg['ws_type'] == 'status'
self.sleep_try(0.2, 10.0, assert_size)
ex_ws.close()
self.ws.close()
示例9: __init__
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def __init__(self, url, password):
super().__init__()
self._url = url
self._password = password
import websocket
websocket.enableTrace(True)
self._ws = websocket.WebSocket(skip_utf8_validation=True)
self._ws.settimeout(10)
self._ws.connect(self._url, timeout=5)
prompt = self._ws.recv()
if prompt != "Password: ":
raise RuntimeError("Expected password prompt, got %r" % prompt)
self._ws.send(self._password + "\r\n")
self._reading_thread = threading.Thread(target=self._keep_reading, daemon=True)
self._reading_thread.start()
示例10: plugin
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def plugin(srv, item):
srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target)
# item.config is brought in from the configuration file
config = item.config
# addrs is a list[] associated with a particular target.
# While it may contain more than one item (e.g. pushover)
# the `websocket' service carries one only, i.e. a ws:// or wss:// uri
uri = item.addrs[0].format(**item.data).encode('utf-8')
# If the incoming payload has been transformed, use that,
# else the original payload
text = item.message
try:
ws = websocket.WebSocket()
ws.connect(uri)
ws.send(text)
ws.close()
except Exception as e:
srv.logging.warning("Cannot write to websocket `%s': %s" % (uri, e))
return False
return True
示例11: testSend
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def testSend(self):
# TODO: add longer frame data
sock = ws.WebSocket()
sock.set_mask_key(create_mask_key)
s = sock.sock = HeaderSockMock("data/header01.txt")
sock.send("Hello")
self.assertEqual(s.sent[0], six.b("\x81\x85abcd)\x07\x0f\x08\x0e"))
sock.send("こんにちは")
self.assertEqual(s.sent[1], six.b("\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc"))
sock.send(u"こんにちは")
self.assertEqual(s.sent[1], six.b("\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc"))
sock.send("x" * 127)
示例12: testRecv
# 需要导入模块: import websocket [as 别名]
# 或者: from websocket import WebSocket [as 别名]
def testRecv(self):
# TODO: add longer frame data
sock = ws.WebSocket()
s = sock.sock = SockMock()
something = six.b("\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc")
s.add_packet(something)
data = sock.recv()
self.assertEqual(data, "こんにちは")
s.add_packet(six.b("\x81\x85abcd)\x07\x0f\x08\x0e"))
data = sock.recv()
self.assertEqual(data, "Hello")