Tiny socket wrapper
Project description
weesocket
Python socket library
About
This library is intended to be used as simple wrapper for working with python low level sockets.
Installing weesocket
# install weesocket library
$ pip install weesocket
# dev version for development:
$ pip install weesocket[dev] # install developer requirements
Using
Use following objects as parents for your custom processing objects.
Server
and Client
objects inherits threading.Thread
module and works in thread mode.
Server socket wrapper
Initialize parameters:
id
- server identification name (type: str | default value:server
)socket_file
- unix socket file (type: str | default value: empty string)host
- host ip (type: str | default value: empty string)port
- host port (type: int | default value:0
)buffer_size
- buffer size for message receiver (type: int | default value:4096
)proxy
- specifying whether server can be used as proxy to send messages between clients (type: bool | default value:True
)secret
- secret to prove server identity (type: bytearray | default value: empty bytearray)remotes
- list of client secrets to check their identities - (type: list of bytearrays | default value: empty list)encrypt
- specifying whether encryption will be enabled (type: bool | default value:True
)rsa_length
- rsa key length - not used if keys given in parameters (type: int | default value:2048
)public_key
- private key string -generated if not given (type: str | default value: empty string)private_key
- private key string - generated if not given (type: str | default value: empty string)daemon
- variable specyfing if thread is daemonized (type: bool | default value:True
)logger
- python logger object (orNone
to disable logging) (type: logger | default value:None
)
Note: one of type parameters socket_file
or host
and port
must be given, if both are given unix socket file is preferred.
Remotes are not used if not given.
Methods:
send
- disconnect from server (data:dict
, target:str
)disconnect
- disconnect client from server (client_id:str
)stop
- stop server listening ()
Note: target
parameter is optional, empty target is processed by server, *
is intended to be used for broadcast to all other clients.
Properties:
id
- id of connection | type:str
enabled
- status of connection | type:bool
Client socket wrapper
Initialize parameters:
id
- server identification name (type: str | default value:server
)socket_file
- unix socket file to connect (type: str | default value: empty string)host
- server host ip to connect (type: str | default value: empty string)port
- server host port to connect (type: int | default value:0
)buffer_size
- buffer size for message receiver (type: int | default value:4096
)secret
- secret code to prove identity (type: bytearray | default value: empty bytearray)remote_secret
- secret code to check server identity (type: list of strings | default value: empty list)encrypt
- specifying whether encryption will be enabled (type: bool | default value:True
)rsa_length
- rsa key length - not used if keys given in parameters (type: int | default value:2048
)public_key
- private key string -generated if not given (type: str | default value: empty string)private_key
- private key string - generated if not given (type: str | default value: empty string)daemon
- variable specyfing if thread is daemonized (type: bool | default value:True
)logger
- python logger object (orNone
to disable logging) (type: logger | default value:None
)
Note: one of type parameters socket_file
or host
and port
must be given, if both are given unix socket file is preferred.
Remotes are not used if not given.
Methods:
send
- disconnect from server (data:dict
, target:str
)disconnect
- disconnect from server ()
Note: target
parameter is optional, empty target is processed by server, *
is intended to be used for broadcast to all other clients and **
is intended to be used for broadcast to all other clients and process by server.
Properties:
id
- id of connection | type:str
enabled
- status of connection | type:bool
Example
Simple examples of server and client socket wrappers.
Server example:
from weesocket import ServerWrapper
class ServerWrapper(Server):
def _trigger(self, payload, sender):
print("sender:", sender)
print("payload:", payload)
server = ServerWrapper(socket_file="/tmp/socket_file.sock")
Client example:
from weesocket import client
class ClientWrapper(Client):
def _trigger(self, payload, sender):
print("sender:", sender)
print("payload:", payload)
client = ClientWrapper(socket_file="/tmp/socket_file.sock")
More examples in tests folder.
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
File details
Details for the file weesocket-2.0.2.tar.gz
.
File metadata
- Download URL: weesocket-2.0.2.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5494af96cacb10c5e14ee99114fce795a47c7a42d143e103cdc18c582df59f8d |
|
MD5 | 085fa209395f9b4d5f7899ce066da047 |
|
BLAKE2b-256 | d189faf36291a4672ea57fd9a47a0d99348db1f01873897be64d3d8ef4213b60 |