Skip to main content

Python socket for machine learning data

Project description

mlsocket

PyPI version image Build Status License: MIT

MLSocket is a python package built on top of the python package of socket to easily send and receive numpy arrays and machine learning models through tcp connection.

  • Free software: MIT license

MLSocket class inherits a built-in socket package of python and supports all of the basic operations of the socket class. It can not only send the byte data but also send numpy arrays and keras models.


Installation

$ pip install mlsocket --user

Features

System Linux macOS Windows
Status Unit Test (Ubuntu) Unit Test (macOS) Unit Test (Windows)
  • Supports Windows 10, Ubuntu, and macOs => Tested on github action
  • Supports data transfer of numpy arrays
  • Supports data transfer of keras models as hdf5 files
  • Supports data transfer of scikit-learn classifiers
  • Support python 3.6, 3.6, 3.8 => Tested on github action

MLSocket inherits the socket object of the built-in python socket, but overrides 4 methods.

  • MLSocket.send(data), MLSocket.sendall(data): Sends the data. Unlike socket.send, thses two functions always show blocking behavior that it waits for the reponse from the server that the server received all the data sent.
  • MLSocket.recv(bufsize): Receives data of the given size, but the return value is not in the size of bufsize. It will receive all the data sent by the client, with the step size of a given bufsize.
  • MLSocket.accept(): This function is just the same as socket.accept(), but returns the MLSocket object and the connected address.

Usage

The basic usage of the MLSocket is demonstrated by the following scripts of server and client which sends and receives numpy array and machine learning models.

Server-Side

from mlsocket import MLSocket

HOST = '127.0.0.1'
PORT = 65432

with MLSocket() as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, address = s.accept()

    with conn:
        data = conn.recv(1024) # This will block until it receives all the data send by the client, with the step size of 1024 bytes.
        model = conn.recv(1024) # This will also block until it receives all the data.
        clf = conn.recv(1024) # Same

Client-Side

from mlsocket import MLSocket
from keras.models import Sequential
from keras.layers import Dense
from sklearn import svm
import numpy as np

HOST = '127.0.0.1'
PORT = 65432

# Make an ndarray
data = np.array([1, 2, 3, 4])

# Make a keras model
model = Sequential()
model.add(Dense(8, input_shape=(None, 4)))
model.compile(optimizer='adam', loss='binary_crossentropy')

# Make a scikit-learn classifier
clf = svm.SVC(gamma=0.00314)

# Send data
with MLSocket() as s:
    s.connect((HOST, PORT)) # Connect to the port and host
    s.send(data) # After sending the data, it will wait until it receives the reponse from the server
    s.send(model) # This will wait as well
    s.send(clf) # Same

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

mlsocket-0.1.2.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

mlsocket-0.1.2-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file mlsocket-0.1.2.tar.gz.

File metadata

  • Download URL: mlsocket-0.1.2.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.5

File hashes

Hashes for mlsocket-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6f3088211bda6348ed675236d9820a71245c2fff189aa4e8459dd2312505e413
MD5 7a6f1c3ee5598857a3e81140b53598e4
BLAKE2b-256 252aeb96aae36f859a65d272b4fc98134727db940918f06b10c046540034f45a

See more details on using hashes here.

File details

Details for the file mlsocket-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: mlsocket-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.5

File hashes

Hashes for mlsocket-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 405ee839839a0fac4b6cd183d8f5e17908fdfe0ee86d5b365ea48770ca47281e
MD5 76ec728ea22ac7081ac7c000cd771355
BLAKE2b-256 1c24da7ae488fcad0324e23b3f93fe1528c87a3892ee9cabbe9ae0a26617d1eb

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