Skip to main content

A Python module for sending data across TCP sockets

Project description

PyDataSocket

This module provides an easy to use python implementation of TCP Sockets for sending and receiving data. This module tries to reduce the effort for the user in determining how to package the data to send and dealing with the socket setting and full package length, ect. These sockets have a few different modes as outlined below:

  • RAW - This mode expects the data to be sent to already be a bytes object. This can be done using tools such as the struct module or numpy.tostring() for numpy data.
  • JSON - This mode will automatically try to jsonize anything that is given to send/receive. This works well for varying data types (dictionaries with strings and numbers). This mode will slow down quite a bit if large messages are passed (i.e. 100x100 list of numbers).
  • NUMPY - This mode expects anything that can be converted to a numpy array using np.asarray() or a dictionary of the same (i.e. {'array1': np.array, 'array2': np.array}). This mode is better to use for sending large arrays, but it is still a little slow because it creates and sends a full numpy file.
  • HDF - This operates similarly to the NUMPY mode, but uses the H5py package instead.

See the examples for how to use. Here you will also find matlab and simulink examples to pair with sending data between python and matlab/simulink. The matlab versions of the TCPReceive/TCPSend sockets must be copied and added to matlab yourself. These only support the RAW and JSON formats.

Install

  • pip install PyDataSocket

The matlab files can be installed to a specific directory using the install_matlab_socket_files(destination) function, where destination is the directory to install the files to.

Usage

from DataSocket import TCPReceiveSocket, TCPSendSocket, RAW, JSON, HDF, NUMPY, install_matlab_socket_files

These sockets are meant to bind to a single network ip and port (i.e. 1 SendSocket connects to 1 ReceiveSocket). The exception to this is that when the TCPSendSocket is configured as a server (default setting) multiple TCPReceiveSockets may connect and each will receive the data. The sockets must be started after creation using start() and this may be set to block the calling script until connection by passing blocking=True to the start function.

The send socket is very simple in that it will take care of everything when data is passed to the send_data() method. The receiving socket requires a handling function to be passed on construction to handler_function. This function will be called everytime data is received and should expect one input (the entire data message, already decoded if using a mode other than RAW).

TCPSendSocket

class TCPSendSocket(object):
    def __init__(self,
                 tcp_port,
                 tcp_ip='localhost',
                 send_type=NUMPY,
                 verbose=True,
                 as_server=True,
                 include_time=False,
                 as_daemon=True):
"""
        A TCP socket class to send data to a specific port and address.
        :param tcp_port: TCP port to use.
        :param tcp_ip: ip address to connect to.
        :param send_type: This is the data type used to send the data. DataSocket.NUMPY uses a numpy file to store the
               data for sending. This is ideal for large arrays. DataSocket.JSON converts the data to a json formatted string.
               JSON is best for smaller messages. DataSocket.HDF uses the HDF5 file format and performance is probably
               comparable to NUMPY. DataSocket.RAW expects a bytes object and sends it directly with no processing. The
               receiving socket must be manually set to receive raw data.
        :param verbose: Whether or not to print errors and status messages.
        :param as_server: Whether to run this socket as a server (default: True) or client. When run as a server, the
               socket supports multiple clients and sends each message to every connected client.
        :param include_time: Appends time.time() value when sending the data message.
        :param as_daemon: runs the underlying threads as daemon.
        """

TCPReceiveSocket

class TCPReceiveSocket(object):
    def __init__(self,
                 tcp_port,
                 handler_function=None,
                 tcp_ip='localhost',
                 verbose=True,
                 as_server=False,
                 receive_as_raw=False,
                 receive_buffer_size=4095,
                 as_daemon=True):
        """
        Receiving TCP socket to be used with TCPSendSocket.
        :param tcp_port: TCP port to use.
        :param handler_function: The handle to a function that will be called everytime a message is received. Must take
               one parameter that is the message. The message is exactly what was sent from TCPSendSocket.
               example:
                        def my_handler(received_data):
                            print(received_data)
        :param tcp_ip: ip address to connect to.
        :param verbose: Whether or not to print errors and status messages.
        :param as_server: Whether to run this socket as a server (default: False) or client. This needs to be opposite
                          whatever the SendSocket is configured to be.
        :param receive_as_raw: Whether or not the incoming data is just raw bytes or is a predefined format (JSON, NUMPY, HDF)
        :param receive_buffer_size: available buffer size in bytes when receiving messages
        :param as_daemon: runs underlying threads as daemon.
        """

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

PyDataSocket-0.0.52.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

PyDataSocket-0.0.52-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file PyDataSocket-0.0.52.tar.gz.

File metadata

  • Download URL: PyDataSocket-0.0.52.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/0.0.1 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.7.9

File hashes

Hashes for PyDataSocket-0.0.52.tar.gz
Algorithm Hash digest
SHA256 2265fabfe8adbec753e148cd8210e2928d1c611077b6c04ac699ec7cc381c064
MD5 b5c5a864575b29107daedcdeef7ce37c
BLAKE2b-256 f387f4e54a4a5ccc071406b3e20bc3b26af7e4d54a11ca0aa0cc3feb3127d2af

See more details on using hashes here.

File details

Details for the file PyDataSocket-0.0.52-py3-none-any.whl.

File metadata

  • Download URL: PyDataSocket-0.0.52-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/0.0.1 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.7.9

File hashes

Hashes for PyDataSocket-0.0.52-py3-none-any.whl
Algorithm Hash digest
SHA256 06084c8948a87b3490f15b1bf21f35069dcfcc1dd582d89ca9de2b4c39f20fd9
MD5 addd2fda99c041340a0b0b2d572c0370
BLAKE2b-256 3fd6eff80511bade7fe6f70de82a6ce49d181e054fedf0850648ffd70fc16035

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