Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
)
)
closed = False
while not closed:
try:
data: Optional[bytes] = sock.recv(65535)
except CONNECTION_EXCEPTIONS:
data = None
connection.receive_data(data or None)
out_data = b""
for event in connection.events():
if isinstance(event, Message):
out_data += connection.send(
Message(data=event.data, message_finished=event.message_finished)
)
elif isinstance(event, Ping):
out_data += connection.send(event.response())
elif isinstance(event, CloseConnection):
closed = True
out_data += connection.send(event.response())
# else:
# print("??", event)
if out_data is None:
break
try:
sock.sendall(out_data)
except CONNECTION_EXCEPTIONS:
closed = True
break
async def receive(self) -> List[Message]:
data = await self.client_stream.receive_some(2 ** 16)
self.connection.receive_data(data)
return [event for event in self.connection.events()]
target="%s?%s" % (uri.path, uri.query),
extensions=[PerMessageDeflate()],
)
)
)
closed = False
while not closed:
try:
data: Optional[bytes] = sock.recv(65535)
except CONNECTION_EXCEPTIONS:
data = None
connection.receive_data(data or None)
out_data = b""
for event in connection.events():
if isinstance(event, Message):
out_data += connection.send(
Message(data=event.data, message_finished=event.message_finished)
)
elif isinstance(event, Ping):
out_data += connection.send(event.response())
elif isinstance(event, CloseConnection):
closed = True
out_data += connection.send(event.response())
# else:
# print("??", event)
if out_data is None:
break
try:
sock.sendall(out_data)
except CONNECTION_EXCEPTIONS:
closed = True
closed = False
while not closed:
try:
data: Optional[bytes] = sock.recv(65535)
except socket.error:
data = None
ws.receive_data(data or None)
outgoing_data = b""
for event in ws.events():
if isinstance(event, Request):
outgoing_data += ws.send(
AcceptConnection(extensions=[PerMessageDeflate()])
)
elif isinstance(event, Message):
outgoing_data += ws.send(
Message(data=event.data, message_finished=event.message_finished)
)
elif isinstance(event, Ping):
outgoing_data += ws.send(event.response())
elif isinstance(event, CloseConnection):
closed = True
if ws.state is not ConnectionState.CLOSED:
outgoing_data += ws.send(event.response())
if not data:
closed = True
try:
sock.sendall(outgoing_data)
except socket.error:
# Negotiate new WebSocket connection
print("Accepting WebSocket upgrade")
out_data += ws.send(AcceptConnection())
elif isinstance(event, CloseConnection):
# Print log message and break out
print(
"Connection closed: code={} reason={}".format(
event.code, event.reason
)
)
out_data += ws.send(event.response())
running = False
elif isinstance(event, TextMessage):
# Reverse text and send it back to wsproto
print("Received request and sending response")
out_data += ws.send(Message(data=event.data[::-1]))
elif isinstance(event, Ping):
# wsproto handles ping events for you by placing a pong frame in
# the outgoing buffer. You should not call pong() unless you want to
# send an unsolicited pong frame.
print("Received ping and sending pong")
out_data += ws.send(event.response())
else:
print("Unknown event: {!r}".format(event))
# 4) Send data from wsproto to network
print("Sending {} bytes".format(len(out_data)))
stream.send(out_data)
async def send(self, data):
if self.closed:
raise WebsocketClosedError()
await self.outgoing.put(Message(data=data))
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect((host, port))
# 1) Negotiate WebSocket opening handshake
print("Opening WebSocket")
ws = WSConnection(ConnectionType.CLIENT)
# Because this is a client WebSocket, we need to initiate the connection
# handshake by sending a Request event.
net_send(ws.send(Request(host=host, target="server")), conn)
net_recv(ws, conn)
handle_events(ws)
# 2) Send a message and display response
message = "wsproto is great"
print("Sending message: {}".format(message))
net_send(ws.send(Message(data=message)), conn)
net_recv(ws, conn)
handle_events(ws)
# 3) Send ping and display pong
payload = b"table tennis"
print("Sending ping: {!r}".format(payload))
net_send(ws.send(Ping(payload=payload)), conn)
net_recv(ws, conn)
handle_events(ws)
# 4) Negotiate WebSocket closing handshake
print("Closing WebSocket")
net_send(ws.send(CloseConnection(code=1000, reason="sample reason")), conn)
# After sending the closing frame, we won't get any more events. The server
# should send a reply and then close the connection, so we need to receive
# twice: