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.5.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

PyDataSocket-0.0.5-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: PyDataSocket-0.0.5.tar.gz
  • Upload date:
  • Size: 7.9 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.5.tar.gz
Algorithm Hash digest
SHA256 714eb2b9dcac03c0890b40781e3d38a681d7e12ff0d0c7f609f3d87ac243510a
MD5 d21b5646a5fb1723bf56c9aed35acfc9
BLAKE2b-256 a31218a45396eec7edb84676a3248b3d97fbd049810a25bf976a96abedd4456b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: PyDataSocket-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 14.6 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6be4fe4103af6fb644933ba90c92ada807f55fa5b9a1e20917281e51aa16cb18
MD5 b367ca75be70ee002effefeee84ac07e
BLAKE2b-256 207ae399ebce0457300e2b4ffd3c19cf207958805d424ba40f72b933d58fa329

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