Send arbitrary Python objects via websockets
Project description
SocketMan
- 对 websockets 包的易用封装。
- 使用起来非常简单
- 可以对发送内容简单加密和压缩
- 可以用它发送任何python对象包括自定义的类
- 注意!上述操作存在安全风险
简单示例
服务端
# 建立一个回调函数处理接收的内容
def returnmsg(msg, conn):
# 这个函数将接收的消息原路返回
print(f'send back msg:{msg}')
conn.send(msg)
# 启动一个服务端
serv = socketman.createServer(port=5010, onrecv=returnmsg)
# 关闭服务端
serv.close()
客户端
# 建立一个回调函数处理接收的内容
def printmsg(msg, conn):
# 这个函数将接收的消息显示出来
print(f'recievemsg:{msg}')
# 连接到服务端uri
conn = socketman.connect(uri = "ws://127.0.0.1:5010", onrecv=printmsg)
# 发送一个自定义类(类是通过pickle封装的,所以接收端需要有该类的定义才能正确接收)
class UserClass:
n = 1
c = UserClass()
conn.send(c)
# 关闭连接
conn.close()
Tips
- 开启AES加密需要在服务端和客户端设置相同的password,例如:
serv = socketman.createServer(port=5010, password='abc')
conn = socketman.connect(uri = "ws://127.0.0.1:5010", password='abc')
- 不加密发送纯字符串不会进行封装,可以当作标准的websocket使用,但开启加密或发送python对象仅支持本库的服务端和客户端。
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
socketman-0.2.0.tar.gz
(11.1 kB
view details)
Built Distribution
socketman-0.2.0-py3-none-any.whl
(13.5 kB
view details)
File details
Details for the file socketman-0.2.0.tar.gz
.
File metadata
- Download URL: socketman-0.2.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82c189e6d928e63cd75d640dacda3e8a21edef4ebb4b04a8608d42bcbbef524d |
|
MD5 | 52407ad47f5681277cddc465f6306d88 |
|
BLAKE2b-256 | b8d21def97d8ae382582fcf89ee88997b7e40ff0088749e1116d657b14a78383 |
File details
Details for the file socketman-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: socketman-0.2.0-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd27ca7ffbc689c943d9f8f1c60dbb95e93de952d17f76e3a6e59e86c8d659e9 |
|
MD5 | 97d66ad29c024f28b90816b6f6317b85 |
|
BLAKE2b-256 | bda5ea81dedb910373ed0fb180a62a772cbbd6da67a485b2cd72c40c5c035705 |