File tree Expand file tree Collapse file tree 6 files changed +69
-10
lines changed Expand file tree Collapse file tree 6 files changed +69
-10
lines changed Original file line number Diff line number Diff line change @@ -60,13 +60,24 @@ list of features provided by each implementation.
6060 implementation, and then some. If you're using the historical implementation,
6161 you should :doc: `ugrade to the new implementation <howto/upgrade >`.
6262
63- Here's an echo server using the :mod: ` asyncio ` API:
63+ Here's an echo server and corresponding client.
6464
65- .. literalinclude :: ../example/echo.py
65+ .. tab :: asyncio
6666
67- Here's a client using the :mod: ` threading ` API:
67+ .. literalinclude :: ../example/asyncio/echo.py
6868
69- .. literalinclude :: ../example/hello.py
69+ .. tab :: threading
70+
71+ .. literalinclude :: ../example/sync/echo.py
72+
73+ .. tab :: asyncio
74+ :new-set:
75+
76+ .. literalinclude :: ../example/asyncio/hello.py
77+
78+ .. tab :: threading
79+
80+ .. literalinclude :: ../example/sync/hello.py
7081
7182Don't worry about the opening and closing handshakes, pings and pongs, or any
7283other behavior described in the WebSocket specification. websockets takes care
Original file line number Diff line number Diff line change 11#!/usr/bin/env python
22
3+ """Echo server using the asyncio API."""
4+
35import asyncio
46from websockets .asyncio .server import serve
57
8+
69async def echo (websocket ):
710 async for message in websocket :
811 await websocket .send (message )
912
13+
1014async def main ():
11- async with serve (echo , "localhost" , 8765 ):
12- await asyncio .get_running_loop ().create_future () # run forever
15+ async with serve (echo , "localhost" , 8765 ) as server :
16+ await server .serve_forever ()
17+
1318
14- asyncio .run (main ())
19+ if __name__ == "__main__" :
20+ asyncio .run (main ())
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+
3+ """Client using the asyncio API."""
4+
5+ import asyncio
6+ from websockets .asyncio .client import connect
7+
8+
9+ async def hello ():
10+ async with connect ("ws://localhost:8765" ) as websocket :
11+ await websocket .send ("Hello world!" )
12+ message = await websocket .recv ()
13+ print (message )
14+
15+
16+ if __name__ == "__main__" :
17+ asyncio .run (hello ())
Original file line number Diff line number Diff line change 1+ [lint .isort ]
2+ no-sections = true
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+
3+ """Echo server using the threading API."""
4+
5+ from websockets .sync .server import serve
6+
7+
8+ def echo (websocket ):
9+ for message in websocket :
10+ websocket .send (message )
11+
12+
13+ def main ():
14+ with serve (echo , "localhost" , 8765 ) as server :
15+ server .serve_forever ()
16+
17+
18+ if __name__ == "__main__" :
19+ main ()
Original file line number Diff line number Diff line change 11#!/usr/bin/env python
22
3- import asyncio
3+ """Client using the threading API."""
4+
45from websockets .sync .client import connect
56
7+
68def hello ():
79 with connect ("ws://localhost:8765" ) as websocket :
810 websocket .send ("Hello world!" )
911 message = websocket .recv ()
10- print (f"Received: { message } " )
12+ print (message )
13+
1114
12- hello ()
15+ if __name__ == "__main__" :
16+ hello ()
You can’t perform that action at this time.
0 commit comments