Skip to main content

gsocket

一个仅 Python 使用的基于 socket 协议的模块

  • SocketServer:连接服务端
  • SocketClient:连接客户端
  • SocketReceiver:自定义 on_open 等

注意:

  • SocketServer、SocketClient、SocketReceiver 自带 send、sendall、sendfile 方法
  • 本模块未匹配 websocket 协议,所有通信基于 struct 防止粘包,所以不适用

安装

pip install gsocket

SocketServer

from gsocket import SocketServer

SocketServer().start_server()

SocketClient

推荐使用 with 上下文,不然需要手动关闭

from gsocket import SocketClient

with SocketClient() as s:
    s.send(msg="你好")

SocketReceiver

SocketServer、SocketClient 都具备此参数,可以自由定义其接收方法 下面以 SocketServer 示例

from typing import Generator
from gsocket import SocketServer, SocketReceiver


class MySocketReceiver(SocketReceiver):
    def on_open(self):
        pass

    def on_message(self, msg: bytes):
        pass

    def on_file(self, filename: str, file_iterator: Generator):
        pass

    def on_close(self):
        pass


SocketServer(receiver=MySocketReceiver).start_server()

Metadata

Release files for gsocket 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gsocket 0.0.2
File Size Uploaded
gsocket-0.0.2.tar.gz 6.5 kB Details

Release files / gsocket-0.0.2.tar.gz

Download URL gsocket-0.0.2.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ace69800f0b0e1bc801f5faf3a7554dd0acb5bf15ab690e7c9e8aa45388dca8f
BLAKE2b-256 checksum
How to use checksums
5e1c69531c5256b0b542a0e11ac66ba3e2eac040277f3086f9f9567f89ab34dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release history Release notifications | RSS feed

This release

0.0.2 This release

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page