Skip to main content

Send arbitrary Python objects via websockets

Project description

SocketMan

  • websockets 包的易用封装。
  • 使用起来非常简单
  • 可以对发送内容简单加密和压缩
  • 可以用它发送任何python对象包括自定义的类
  • 注意!上述操作存在安全风险

安装

pip install socketman

简单用例

服务端

# 建立一个回调函数处理接收的内容
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.2.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

socketman-0.2.2-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file socketman-0.2.2.tar.gz.

File metadata

  • Download URL: socketman-0.2.2.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for socketman-0.2.2.tar.gz
Algorithm Hash digest
SHA256 ec4ee0bdc5fd3b14125f4a8209d99e6f80b9f9e94783dfbe9a847198967a9cf8
MD5 f1a0f6826bd72dd662cc98ea4e0c4929
BLAKE2b-256 19e29bdef65aaaddf1e3095f9b993bd2fb215c607702847170da54cfc21f42c7

See more details on using hashes here.

File details

Details for the file socketman-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: socketman-0.2.2-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

Hashes for socketman-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8b444b626f287e7d8e7cca158f9f940cb43f1e0c90ebabd843bc036d34fb041f
MD5 55eeabed4ffebd116e9c5866e931d327
BLAKE2b-256 b6bb917b78e4d3a1bd6af6e89b43129a0a39bbb22c21d567d26c8db3373b913a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page