Skip to main content

SimpleTCPClient

import random
import time
from simple_socket.tcp_client import SimpleTCPClient # try also SimpleSSLClient for semi-secure communication (it does not verify the cert)

client = SimpleTCPClient('192.168.254.254', 1885)
print('client.Hostname=', client.Hostname)

# by default, the client will connect and attempt to maintain its connection.
# if you want to manually control the connection, pass autoConnect=False
# then use the .Connect() and .Disconnect() methods to manage your connection manually

client.onConnected = lambda _, state: print('The client is', state)
client.onDisconnected = lambda _, state: print('The client is', state)

client.onReceive = lambda _, data: print('Rx:', data)

while True:
    cmd = random.choice(['q', 'n', 'i'])
    print('sending:', cmd)
    if 'Connected' in client.ConnectionStatus:
        client.Send(cmd)
    time.sleep(5)

SimpleUDPClient

from simple_socket.udp_client import SimpleUDPClient
import time

client1 = SimpleUDPClient('localhost', sendIPPort=1025, receiveIPPort=1024)


def Client1HandleReceive(interface, data):
    client1.Send('From client1 echo: {}'.format(data.decode()))


client1.onReceive = Client1HandleReceive
print('client1=', client1)

client2 = SimpleUDPClient('localhost', sendIPPort=1024, receiveIPPort=1025)
client2.onReceive = lambda _, data: print('Rx:', data)
print('client2=', client2)
while True:
    client2.Send(f'From client2: The time is {time.asctime()}\r\n')
    time.sleep(3)

SimpleTCPServer

from simple_socket.tcp_server import SimpleTCPServer # try also SimpleSSLServer for semi-secure communication (it uses a self-signed cert)
server = SimpleTCPServer(3888)

server.onConnected = lambda client, state: print('Client {} is {}'.format(client, state))
server.onDisconnected = lambda client, state: print('Client {} is {}'.format(client, state))


def HandleRx(client, data):
    print('Server received {} from {}'.format(data, client))
    client.Send(b'Echo: ' + data + b'\r\n')

    if b'q' in data:
        # the server can force-close a connection
        # in this case,
        # the server will disconnect a client if they send the letter 'q'
        client.Disconnect()


server.onReceive = HandleRx

Notes

You can pass the parameter “listenAddress” to a Server init. This will allow you to listen to

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_socket-0.0.7.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

simple_socket-0.0.7-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file simple_socket-0.0.7.tar.gz.

File metadata

  • Download URL: simple_socket-0.0.7.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for simple_socket-0.0.7.tar.gz
Algorithm Hash digest
SHA256 49be82b5b69653484d66654f1df7ff963013299b622da6229e0b6317094f419d
MD5 9e3a67e0f22fd5a4c18a7c35e4161ba8
BLAKE2b-256 521a630d294676054795f370cc044a25401c45fcce51a3302c3d568e48ec0582

See more details on using hashes here.

File details

Details for the file simple_socket-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: simple_socket-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for simple_socket-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 533a02a8b4a865b0409512a20cb4d323359c0c73482d58b512a04e07902ed657
MD5 f477e6148c004ab59be670dc09654347
BLAKE2b-256 9370b38c121cbb18e8fb9ff2fee1c75985f021c1f95f1092e294d7471399a5fb

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