1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# !/usr/bin/env python

# WS server that sends messages at random intervals

import asyncio
import datetime
import random
import websockets

async def time(websocket, path):
while True:
now = datetime.datetime.utcnow().isoformat() + 'Z'
await websocket.send(now)
await asyncio.sleep(random.random() * 3)

start_server = websockets.serve(time, '127.0.0.1', 5678)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

客户端跑不到。

1
TimeoutError: [Errno 10060] Connect call failed ('xx', 8765)

端口监听成功的:

1
netstat -lnpt

centos7 检查防火墙:

1
firewall-cmd --state

开启状态,检查端口:

1
firewall-cmd --zone=public --list-ports

未开放,开启端口:

1
2
3
4
5
firewall-cmd --zone=public --add-port=5672/tcp --permanent   # 开放5672端口

firewall-cmd --zone=public --remove-port=5672/tcp --permanent # 关闭5672端口

firewall-cmd --reload # 配置立即生效