-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
36 lines (31 loc) · 1.16 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import asyncio
import websockets
from faker import Faker
fake = Faker()
async def send_messages(websocket):
while True:
await asyncio.sleep(1)
await websocket.send(f"{fake.name()}")
await websocket.send(f"{fake.country()}")
await websocket.send(f"{fake.email()}")
await websocket.send(f"{fake.url()}")
await websocket.send(f"{fake.text()}")
await websocket.send(f"{fake.latitude()}")
await websocket.send(f"{fake.currency()}")
await websocket.send(f"{fake.city()}")
async def receive_messages(websocket):
numbering = 1
while True:
message = await websocket.recv()
print(f"{numbering}. {message}")
numbering += 1
async def server(websocket, path):
# Create tasks to send and receive messages concurrently
send_task = asyncio.create_task(send_messages(websocket))
receive_task = asyncio.create_task(receive_messages(websocket))
await asyncio.gather(send_task, receive_task)
async def main():
# Start the server
async with websockets.serve(server, "localhost", 8000):
await asyncio.Future()
asyncio.run(main())