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.1.tar.gz
(10.9 kB
view details)
Built Distribution
socketman-0.2.1-py3-none-any.whl
(13.4 kB
view details)
File details
Details for the file socketman-0.2.1.tar.gz
.
File metadata
- Download URL: socketman-0.2.1.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30b878b9c216fc24a667782ef0179b9ccef8b430dfc29be75c4653744c6c74d8 |
|
MD5 | d7f7e3995e4db9767862fd754a86332b |
|
BLAKE2b-256 | e04769dd76f2f4f2164b81ab013d335863b45c5c14498327c87bc0122cd6f60e |
File details
Details for the file socketman-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: socketman-0.2.1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbf09bd6ade5f5edc10d7f70c1ed4296f8605b0e898224a382fd53e060fc3642 |
|
MD5 | 0318b0eabd834a2d730d3fabd8462fc1 |
|
BLAKE2b-256 | 50cb8d02c25c8d8268e478dbe52a76a8d6afdda673eae360be4e24ac223da18d |