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.
"""
Metadata
Release files for PyDataSocket 0.0.52
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| PyDataSocket-0.0.52.tar.gz | 8.0 kB | Without provenance | Details for PyDataSocket-0.0.52.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| PyDataSocket-0.0.52-py3-none-any.whl | Python 3 | none | any | Details for PyDataSocket-0.0.52-py3-none-any.whl |
Total release size: 22.8 kB
Release files / PyDataSocket-0.0.52.tar.gz
| Download URL | PyDataSocket-0.0.52.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2265fabfe8adbec753e148cd8210e2928d1c611077b6c04ac699ec7cc381c064
|
|
BLAKE2b-256 checksum How to use checksums |
f387f4e54a4a5ccc071406b3e20bc3b26af7e4d54a11ca0aa0cc3feb3127d2af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / PyDataSocket-0.0.52-py3-none-any.whl
| Download URL | PyDataSocket-0.0.52-py3-none-any.whl |
|---|---|
| Size | 14.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
06084c8948a87b3490f15b1bf21f35069dcfcc1dd582d89ca9de2b4c39f20fd9
|
|
BLAKE2b-256 checksum How to use checksums |
3fd6eff80511bade7fe6f70de82a6ce49d181e054fedf0850648ffd70fc16035
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for PyDataSocket 0.0.52
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.