zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【python】python-socketio+firecamp使用踩坑指南

Python 指南 使用
2023-09-11 14:22:09 时间

server.py:

import eventlet
import asyncio

eventlet.monkey_patch()

import socketio
import eventlet.wsgi

sio = socketio.Server(async_mode='eventlet', cors_allowed_origins='*')  # 指明在evenlet模式下


@sio.event
def connect(sid, environ):
    print(f"connect, sid={sid}, environ={environ}")

@sio.event
def disconnect(sid):
    print('disconnect ', sid)

@sio.on('hello')
def hello(sid, data):
    print('server receive:', data)
    print("-----")
    sio.emit('world', {'data': 'world' + data})

app = socketio.Middleware(sio)
eventlet.wsgi.server(eventlet.listen(('127.0.0.1', 9000)), app)

client.py

import socketio

sio = socketio.Client()

@sio.on('connect')
def on_connect():
    print("client connect")

@sio.on('world')
def world(data):
    print('client receive:', data)

sio.connect('http://127.0.0.1:9000')
sio.emit("hello", {"data": "hello"})
print('client send hello success')

server端代码运行成功之后,再运行client端代码,通过log可以看两端通信正常;
server

D:\softwsss/demo2/server.py
(5104) wsgi starting up on http://127.0.0.1:9000

client

D:\software_install\Anaconda3\python.exe D:/python/office_txt/axera/demo2/client.py
client connect
client send hello success
client receive: {'data': 'world'}

server端日志更新

(5104) accepted ('127.0.0.1', 5615)
server receive: {'data': 'hello'}

现在我们用接口调试工具firecamp代替客户端对服务端进行请求操作;
出师不利,刚开始连接就遇到了这么一个报错
在这里插入图片描述

detail:

(13392) accepted ('127.0.0.1', 4824)
The client is using an unsupported version of the Socket.IO or Engine.IO protocols (further occurrences of this error will be logged with level INFO)
127.0.0.1 - - [19/Nov/2022 17:25:56] "GET /socket.io/?EIO=3&transport=polling&t=OIFDNAH HTTP/1.1" 400 219 0.001000

在这里插入图片描述

查看下第三方包的安装版本,主要焦点集中在python-engineio python-socketio
pip list

python-engineio                    4.3.4
python-socketio                    5.7.2

官方作者给的各版本对应的关系:

在这里插入图片描述
refs:

https://pypi.org/project/python-socketio/

更新版本:
安装:

pip install python-socketio==4.6.0
python-engineio                    4.3.4
python-socketio                    4.6.0

查询python-engineio所有版本

pip index versions python-engineio --trusted-host pypi.mirrors.ustc.edu.cn

WARNING: pip index is currently an experimental command. It may be removed/changed in a future release without prio
r warning.
python-engineio (4.3.4)
Available versions: 4.3.4, 4.3.3, 4.3.2, 4.3.1, 4.3.0, 4.2.1, 4.2.0, 4.1.0, 4.0.1, 4.0.0, 3.14.2, 3.14.1, 3.14.0, 3
.13.2, 3.13.1, 3.13.0, 3.12.1, 3.12.0, 3.11.2, 3.11.1, 3.11.0, 3.10.0, 3.9.3, 3.9.2, 3.9.1, 3.9.0, 3.8.2.post1, 3.8
.2, 3.8.1, 3.8.0, 3.7.0, 3.6.0, 3.5.2, 3.5.1, 3.5.0, 3.4.4, 3.4.3, 3.4.2, 3.4.1, 3.4.0, 3.3.2, 3.3.1, 3.3.0, 3.2.3,
 3.2.2, 3.2.1, 3.2.0, 3.1.2, 3.1.1, 3.1.0, 3.0.0, 2.3.2, 2.3.1, 2.3.0, 2.2.0, 2.1.1, 2.1.0, 2.0.4, 2.0.3, 2.0.2, 2.
0.1, 2.0.0, 1.7.0, 1.6.1, 1.6.0, 1.5.4, 1.5.3, 1.5.2, 1.5.1, 1.5.0, 1.4.0, 1.3.2, 1.3.1, 1.3.0, 1.2.4, 1.2.3, 1.2.2
, 1.2.1, 1.2.0, 1.1.2, 1.1.1, 1.1.0, 1.0.4, 1.0.3, 1.0.2, 1.0.1, 1.0.0, 0.9.2, 0.9.1, 0.9.0, 0.8.8, 0.8.7, 0.8.6, 0
.8.5, 0.8.4, 0.8.3, 0.8.2, 0.8.1, 0.8.0, 0.7.2, 0.7.1, 0.7.0, 0.6.9, 0.6.8, 0.6.7, 0.6.6, 0.6.5, 0.6.4, 0.6.3, 0.6.
2, 0.6.1, 0.6.0, 0.5.1, 0.5.0, 0.4.0, 0.3.1, 0.3.0, 0.2.0, 0.1.0
  INSTALLED: 4.3.4
  LATEST:    4.3.4

pip install python-engineio==3.14.0

一开始想找3.6.0版本安装,竟然有个报错,至少支持3.13.0,索性就安装3.14.0这个版本了;
安装时的报错:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This beh
aviour is the source of the following dependency conflicts.
python-socketio 4.6.0 requires python-engineio>=3.13.0, but you have python-engineio 3.6.0 which is incompatible.
Successfully installed python-engineio-3.6.0

安装成功log:
pip install python-engineio==3.14

Looking in indexes: http://pypi.mirrors.ustc.edu.cn/simple/
Collecting python-engineio==3.14
  Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/4c/93/0dbe3e3e43a19c9a0279dde66e15889013682f896187ca777
551de8cc7be/python_engineio-3.14.0-py2.py3-none-any.whl (51 kB)
     ---------------------------------------- 51.7/51.7 KB 531.5 kB/s eta 0:00:00
Requirement already satisfied: six>=1.9.0 in d:\software_install\anaconda3\lib\site-packages (from python-engineio=
=3.14) (1.11.0)
Installing collected packages: python-engineio
  Attempting uninstall: python-engineio
    Found existing installation: python-engineio 3.6.0
    Uninstalling python-engineio-3.6.0:
      Successfully uninstalled python-engineio-3.6.0
Successfully installed python-engineio-3.14.0

最终两个包的版本就是:

python-engineio                   3.14.0
python-socketio                    4.6.0

开启服务端,现在开始尝试用firecamp连接 http://127.0.0.1:9000
就可以正常开启了

firecap也可以正常连接了
在这里插入图片描述

此时也可以与服务端进行正常的通信了
在这里插入图片描述
服务端收到了两次客户端发来的data
在这里插入图片描述
server与firecamp通信正常:
在这里插入图片描述

fastapi+python-socketio

# -- coding:utf-8 --

import socketio
from fastapi import FastAPI

sio = socketio.AsyncServer(
    async_mode="asgi", cors_allowed_origins="*", logger=True, engineio_logger=True
)
app = FastAPI(debug=True)
app_socketio = socketio.ASGIApp(sio)
app.mount(path="/", app=app_socketio)


@sio.event
async def connect(sid, environ):
    print('connect ', sid)
    await sio.emit("Server", {'data': "Connect", 'count': 0})


@sio.on('message')
async def my_message(sid, data):
    print('message ', data)
    await sio.emit("message", {'data': "message" + data})


@sio.event
async def disconnect(sid):
    print('disconnect ', sid)


@sio.event
async def error(sid):
    print('error ', sid)


@app.get("/ss")
async def root():
    return {"message": "Hello World"}


if __name__ == '__main__':
    import uvicorn
    import os

    name_app = os.path.splitext(os.path.basename(__file__))[0]
    uvicorn.run(app=f"{name_app}:app", host="127.0.0.1", port=5008, reload=True, debug=True)

firecamp端:
在这里插入图片描述
服务端:

在这里插入图片描述

先mark一下,在这里走了不少的弯路,悟性太差,还需加油~~~~