Skip to main content

A librairie in python to comunicate more efficiently possible with structures.

Project description

socket-plus

A library in python to communicate as efficiently as possible with structures. to install:
pip install socket-plus
You can have some bugs and is not finished yet but it works.

How to use

you need a list of dictionaries with how is formatted the header of data. Like this:

headers_socket = [
    {
        "name":"id_type",
        "type":int,
        "len":4
    }
]

with this example is mean the first element of the headers have a name like "id_type", the type is int and take 4 bit. All int is mesured by bit, this means this int can go to 0 at 24-1 equal 15 if the given number is over you will get a error on the server or the client.

for the body when you have the main data it is also a list of dict. Like this:

format_socket = {
    0:[
        {
            "name":"len_msg",
            "type":int,
            "len":12
        },
        {
            "name":"msg",
            "type":str,
            "len":"len_msg"
        }
    ]
}

This means when the header is 0 this format is applied for the rest. We have the first element which is an int with name "len_msg" (you can use space) with a len of 12 bit (0 at 212-1 that is 4 095). The second element has a name like "msg" with type str and len "len_msg". With str the len is in byte. One letter is not necessarily one byte for exemple "ç" is two bytes. for len, it is not a number, it is a str, by doing this, this means that it takes the value in data, which means the len of msg is equal to the number of len_msg.

to setup the client, it is this line:

client = socket_plus.Client_connection("localhost", 6000,  headers_socket, format_socket)

to setup the server, it is this line:

server = socket_plus.Server_connection("localhost", 6000, headers_socket, format_socket, update=update)

In first is ip "localhost", it is if you are in the same machine, else it is a str like this: "540.804.530.782". The second argument is the port as an int. The third and four arguments are for headers and the format that you defined before. update is a function to use when a client connect (this means more than one client can connect to the server, server is a multithread this means you need to call server.start to start). For example:

def update(self):
    msg = "test message"
    self.add_to_send({"id_type": 0, "len_msg": len(msg), "msg": msg}) # add to send more details after
    msg = "it's work"
    self.add_to_send({"id_type": 0, "len_msg": len(msg), "msg": msg})
    self.send() # send all info
    print(self.recv()) # print what you receive from client

Inside this example we have some functions. self.add_to_send to add data while respecting the header that you gave before, and for the format it is the same. This means you send an id_type 0 with len_msg equal to the len of msg and with msg equal to msg that you have defined before. for client it is client.add_to_send also. self.send() to send all data you entered before, same for client. and self.recv to receive from client, for client it is the same but you receive from the server.

More about

The main server is a thread, and other connections generates new threads so if you want to stop, you need to call server.stop(). So it generates a "bug" if the main thread (the place where the main code is executed) crash, other thread will continue to run so you need to force to quit with task manager or other.

when you init you can give other arguments if you have two protocols, one for the server and the other for the client. Like this :

socket_plus.Client_connection("localhost", 6000, client_header=c_headers, client_format=c_formats, server_header=s_headers, server_format= s_format)

For client and servers, it is :

socket_plus.Server_connection("localhost", 6000, server_header=s_headers, server_format=s_formats, client_header=c_headers, client_format=c_format, update=update)

when s_headers is header of server it is how server send data and s_formats is same but it is for the format c_headers, it is the same for clients and also for c_format. If you have a suggestion it is here. If you have a question we can talk here.

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

socket-plus-0.0.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

socket_plus-0.0.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file socket-plus-0.0.2.tar.gz.

File metadata

  • Download URL: socket-plus-0.0.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for socket-plus-0.0.2.tar.gz
Algorithm Hash digest
SHA256 72e751f2cb916ca3cad7e35b8c9cff62fd27abec257c22004a62f956d1cc51ab
MD5 0c35089dbf33e8deb5abe7120bb4bfa0
BLAKE2b-256 03f3ee61231d8cff20af6935e72407d7fbb86a4328a457b51358d549186cba87

See more details on using hashes here.

File details

Details for the file socket_plus-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: socket_plus-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for socket_plus-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dc389c21cde18497d1895c3d9e22d55d8292883865b9246380214ea4a5489207
MD5 8ed6187d7f0629188ae4f07fd2ddf941
BLAKE2b-256 4d948d3223a1c6cc0691278fe9e0e1f7fbe405bdf2753bc10367afb5054f27bb

See more details on using hashes here.

Supported by

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