Multithreaded TCP server/client
Project description
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")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sockdot-1.0.1.tar.gz
.
File metadata
- Download URL: sockdot-1.0.1.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a70796c7c4134c09370e6ce27674f5a077b66f06fec583b4dc279b8686ae56b |
|
MD5 | 7e7df4f8222b24efbfff0eef96c91237 |
|
BLAKE2b-256 | 8f3b76719ddab16e319cfe325283a96c457eaefea8110872dac3fa082a3ba942 |
File details
Details for the file sockdot-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: sockdot-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7a6e356d32f5202e44ff235b8d518c64ea56cd12061e08c19246d80d13fce2c |
|
MD5 | 894707d2f7462f576b049bd96988676b |
|
BLAKE2b-256 | db1d2f8a078635b2344d8f940ddeb2aa7339b40ddc3c54d9cbb13e31fee1aa86 |