Skip to main content

A simple wrapper written on top of standard python sockets

Project description

Simplesocks

A simple socket wrapper for python built on top of the standard socket library with password based encryption for security.

Instructions

  1. Install
pip install simplesocks
  1. Create server.py
import socket
from simplesocks.server import SimpleServer
import json

class TestServer(SimpleServer):
    def accept_client_connection(self, client_socket: socket.socket, client_address: tuple, initialization_data: object) -> None:
        super().accept_client_connection(client_socket, client_address, initialization_data)

        initialization_data = json.loads(initialization_data)['id']
        print(f"Client {client_address[0]}:{client_address[1]} connected as {initialization_data}")

    def handle_incoming_data(self, client_id: str, client_socket: socket.socket, data: bytes) -> None:
        super().handle_incoming_data(client_id, client_socket, data)

        if data == b"TERMINATE":
            print(f"Server termination requested by {client_id}")
            self.terminate_server()
        
        self.broadcast_data(client_socket, json.dumps({'id': client_id, 'data': data.decode()}).encode('utf-8'))

    def close_client_connection(self, client_id: str, client_socket: socket.socket) -> None:
        super().close_client_connection(client_id, client_socket)
        print(f"Client {client_id} disconnected")

server = TestServer(server_key=b"msZ5ZR9zjtBXRzssQ3qD33FAVxTBir55rHa1dAJlV5g=")
server.listen()
  1. Create client.py
import json
import threading
import time
from simplesocks.client import SimpleClient, ClientReceiveDataTimeoutException

client = SimpleClient('192.168.1.200', 4444, server_key=b"msZ5ZR9zjtBXRzssQ3qD33FAVxTBir55rHa1dAJlV5g=")

def receive():
    while True:
        try:
            message = json.loads(client.receive_data().decode('utf-8'))
            print(f"{message['id']}: {message['data']}")
        except ClientReceiveDataTimeoutException as e:
            continue
def write():
    while True:
        client.send_data(input("").encode('utf-8'))

receive_thread = threading.Thread(target=receive)
receive_thread.start()

write_thread = threading.Thread(target=write)
write_thread.start()
  1. Start the server
python server.py
  1. Connect with client instances
python client.py

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

simplesocks-1.0.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

simplesocks-1.0.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file simplesocks-1.0.0.tar.gz.

File metadata

  • Download URL: simplesocks-1.0.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for simplesocks-1.0.0.tar.gz
Algorithm Hash digest
SHA256 cb6676250dc43309b4d6f390c2b723ccba7cdc03df957e8a5578b94d2aa9b997
MD5 c84a8fe4b38aea484c40f6b2f5a39882
BLAKE2b-256 e064f961574ecf736e98246c787ee72fc96109293d95bcbe9d8947240b9b51a0

See more details on using hashes here.

File details

Details for the file simplesocks-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: simplesocks-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for simplesocks-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ab0567f121c863313135ba14cca1a2339f53bc736540a2d14ddf8374cd271712
MD5 2ec2d89c37a46c8da6cd9f7674c5f48d
BLAKE2b-256 6368c9ca3d81bd712b71e8a86c5ff2f1ccce27fe3bcd686b29dec8aeade04837

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