sockdot

Multithreaded TCP server/client


Keywords
socket client server multithreaded
License
MIT
Install
pip install sockdot==1.0.1

Documentation

Sockdot

simplified tcp networking library

note: This is not a websocket.

sockdot allows you to create server/client applications without having to use web-standard protocols in your application. the library is a threaded tcp socket and allows for events to be used, making it easy to inpliment in server/client application. i created this library to meet my needs in a Lan software project, so 't could be of use to someone else :).

Installation

pip install sockdot

Installing from source.

git clone https://github.com/rubbiekelvin/sockdot.git
cd sockdot
python setup.py sdist bdist_wheel
pip install dist\sockdot-1.0.1-py3-none-any.whl

Usage

server.py

from sockdot import Server
from sockdot.events import Event

serverevents = Event()
server = Server(debug=True)

@serverevents.event
def on_data_recieved(client, data):
	print("recieved:", data)
	server.send(client, f"you said {data}")

@serverevents.event
def on_connection_open(client):
	print(f"client {client} joined")

@serverevents.event
def on_connection_close(client):
	print(client, "closed connection")

@serverevents.event
def on_server_destruct():
	print("server shutdown")

@serverevents.event
def on_error(exception, message):
	# print(f"error {exception} occured, message:", message)
	pass

@serverevents.event
def on_port_changed(port):
	print("server changed port to", port)

@serverevents.event
def on_running_changed(running):
	print("server is running" if running else "server is not running")

server.updateevent(serverevents)
server.run()

the server's runs on the machine's host name

from sockdot import host
print(host())
# outputs ["host_name", "host_ip"]

client.py

import time, threading
from sockdot import Client
from sockdot.events import Event

clientevents = Event()
client = Client(host="rubbie-io", debug=True)

def start(connected):
	if connected:
		for i in range(10):
			client.send(str(i))
			time.sleep(4)
		client.close()

@clientevents.event
def on_data_recieved(data):
	print(f"got {data} from server")

@clientevents.event
def on_connected_changed(connected):
	threading.Thread(target=start, args=(connected,)).start()

@clientevents.event
def on_error(exception, message):
	print(f"error {exception} occured, message:", message)

@clientevents.event
def on_host_changed(host):
	pass

@clientevents.event
def on_port_changed(port):
	pass

@clientevents.event
def on_handshake_started():
	pass

@clientevents.event
def on_handshake_ended(result):
	pass

client.updateevent(clientevents)
client.connect()

Adding authenthecation

create a file ".auth", could be anything you want, but in my case, i named it ".auth". the file contains keys and values of security parameters in json format.

{
	"SECURITY_KEY" : "secret key",
	"WHITELIST": [],
	"BLACKLIST": [],
	"USE_WHITELIST": false
}

in server.py, make this change:
note that it is also possible for auth settings to be in a python dictionary, use could use any one you want. the auth keyword argument can be a str (filename) type or dict (auth dictionary).

# from file...
server = Server(debug=True, auth=".auth")

# from dictionary
server = Server(debug=True, auth={
	"SECURITY_KEY" : "secret key",
	"WHITELIST": [],
	"BLACKLIST": [],
	"USE_WHITELIST": False
})

in client.py, make this change:

client.connect(authkey="secret key")