Skip to content

Commit

Permalink
Update websocket and asyncio API uses
Browse files Browse the repository at this point in the history
  • Loading branch information
kyuupichan committed Mar 16, 2024
1 parent 25ae1b0 commit 04f0dd7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 22 deletions.
10 changes: 6 additions & 4 deletions aiorpcx/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
from functools import partial

try:
import websockets
from websockets.client import connect
from websockets.server import serve
from websockets.exceptions import ConnectionClosed
except ImportError:
websockets = None

Expand Down Expand Up @@ -56,7 +58,7 @@ async def ws_server(cls, session_factory, websocket, _path):
@classmethod
async def ws_client(cls, uri, **kwargs):
session_factory = kwargs.pop('session_factory', RPCSession)
websocket = await websockets.connect(uri, **kwargs)
websocket = await connect(uri, **kwargs)
return cls(websocket, session_factory, SessionKind.CLIENT)

async def recv_message(self):
Expand All @@ -70,7 +72,7 @@ async def recv_message(self):
async def process_messages(self):
try:
await self.session.process_messages(self.recv_message)
except websockets.ConnectionClosed:
except ConnectionClosed:
pass

# API exposed to session
Expand Down Expand Up @@ -128,7 +130,7 @@ async def __aexit__(self, exc_type, exc_value, traceback):

def serve_ws(session_factory, *args, **kwargs):
ws_handler = partial(WSTransport.ws_server, session_factory)
return websockets.serve(ws_handler, *args, **kwargs)
return serve(ws_handler, *args, **kwargs)


connect_ws = WSClient
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def find_version(filename):
python_requires='>=3.8',
install_requires=[],
extras_require={
'ws': 'websockets',
'ws': 'websockets>=12.0',
},
packages=['aiorpcx'],
description='Generic async RPC implementation, including JSON-RPC',
Expand Down
17 changes: 2 additions & 15 deletions tests/test_socks.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,23 +475,10 @@ def data_received(self, data):
self.transport.write(self.response)


def localhosts():
candidates = ['127.0.0.1', '::1', 'localhost']
loop = asyncio.get_event_loop()
result = []
for host in candidates:
coro = loop.create_server(FakeServer, host=host, port=50001)
try:
server = loop.run_until_complete(coro)
except OSError as e:
result.append(pytest.param(host, marks=pytest.mark.skip))
else:
server.close()
result.append(host)
return result
localhosts = ['127.0.0.1', '::1', 'localhost']


@pytest.fixture(params=localhosts())
@pytest.fixture(params=localhosts)
def proxy_address(request, event_loop, unused_tcp_port):
host = request.param
coro = event_loop.create_server(FakeServer, host=host, port=unused_tcp_port)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_websocket.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio

import pytest
import websockets

from aiorpcx import *

Expand All @@ -10,7 +9,7 @@

@pytest.fixture
def ws_server(unused_tcp_port, event_loop):
coro = serve_ws(MyServerSession, 'localhost', unused_tcp_port, loop=event_loop)
coro = serve_ws(MyServerSession, 'localhost', unused_tcp_port)
server = event_loop.run_until_complete(coro)
yield f'ws://localhost:{unused_tcp_port}'
if hasattr(asyncio, 'all_tasks'):
Expand Down

0 comments on commit 04f0dd7

Please sign in to comment.