Skip to main content

Client-Server threaded TCP connector. This project also uses socket library.

Project description

Simple threaded TCP

A module for easy TCP threaded connection.

Installation with pip: pip install simple-threaded-tcp

Example code

import socket

from sttcp.server import Server
from sttcp.client import Client

# Creating a TCP server connector object
server = Server('localhost', 62134)

# Defining connection with client behavior
@server.connection_handler
def server_connection_handler(address: tuple, connection: socket.socket):
    if address[0] == '127.0.0.2': # address is a (hostname, port) tuple
        print(f'Connection from {":".join(map(str, address))} declined!')
        return False # Declining client (also allowed to use connection.close())
    print(f'Connected by {":".join(map(str, address))}')
    return True # Accepting client

# Defining receiving information from client behavior
@server.receive_handler
def server_receive_handler(address: tuple, connection: socket.socket, data: bytes):
    received_string = data.decode('utf-8') # Decoding input data
    received_string += '!'
    connection.sendall(received_string.encode('utf-8')) # Sending encoded string to client

# Defining disconnection behavior
@server.disconnection_handler
def server_disconnection_handler(address: tuple):
    print(f'Disconnected by {":".join(map(str, address))}')

# Defining universal behavior
# It being executed at all request phases like Server.HandlerType.connection, Server.HandlerType.receive etc.  
@server.universal_handler
def server_universal_handler(handler_type: Server.HandlerType, address: tuple, connection: socket.socket, data: bytes):
    print('- Server', handler_type, sep=': ')
    return True # If server.connection_handler or something like that is not specified it works instead of them


server.start() # Launching server (execution started in parallel thread)

# Creating a TCP client connector object
client = Client('localhost', 62134)

# Defining connection with server behavior
@client.connection_handler
def client_connection_handler(address: tuple, connection: socket.socket):
    print(f'Connected to {":".join(map(str, address))}')
    # It is required to send anything to server or connection will not finish
    connection.sendall('Meow'.encode('utf-8')) # Sending encoded string to server
    return True # Returning True continues connection and awaits for server response

# Defining receiving response from server
@client.response_handler
def client_response_handler(address: tuple, connection: socket.socket, data: bytes):
    response_string = data.decode('utf-8') # Decoding input data
    print(f'Received string "{response_string}"')
    if response_string == 'Meow!':
        connection.sendall('Really?'.encode('utf-8')) # Sending encoded string to server
        return True # Continuing connection for awaiting future response
    else:
        return False # Stopping connection

# Defining disconnection with server behavior 
@client.disconnection_handler
def client_disconnection_handler(address: tuple):
    print(f'Disconnected from {":".join(map(str, address))}')

# Defining exceptions handling behavior
@client.unconnected_handler
def client_unconnected_handler(address: tuple, exception: Exception):
    print(f'Could not connect to {":".join(map(str, address))}')
    raise exception

# Defining universal behavior
@client.universal_handler
def client_universal_handler(handler_type: Server.HandlerType, address: tuple, connection: socket.socket, data: bytes):
    print('- Client', handler_type, sep=': ')
    return True # The same as the server universal handler return


client.start() # Launching client (execution started in parallel thread)


server.keep_alive()
# server.keep_alive() is only needed to keep thread alive until KeyboardInterrupt will not be raised
# This is optional in cases of you have something like "while True" statement

What's new?

Version 1.0

  • Nothing :)

Version 1.1

  • Now with README.md

Version 1.2

  • Changed Recieved to Received
  • Now sttcp.client.Client.set_handler and sttcp.server.Server.set_handler are deprecated
  • Added new method sttcp.client.Client.connection_handler
  • Added new method sttcp.client.Client.response_handler
  • Added new method sttcp.client.Client.disconnection_handler
  • Added new method sttcp.client.Client.universal_handler
  • Added new method sttcp.client.Client.unconnected_handler
  • Added new method sttcp.server.Server.connection_handler
  • Added new method sttcp.server.Server.receive_handler
  • Added new method sttcp.server.Server.disconnection_handler
  • Added new method sttcp.server.Server.universal_handler
  • Renamed method sttcp.server.Server.mainloop to sttcp.server.Server.keep_alive

Contacts

Discord: @emilahmaboy

that is all :)

P.s

Sorry for my bad english. I am from Russia, yeah

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

simple_threaded_tcp-1.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

simple_threaded_tcp-1.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file simple_threaded_tcp-1.2.tar.gz.

File metadata

  • Download URL: simple_threaded_tcp-1.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for simple_threaded_tcp-1.2.tar.gz
Algorithm Hash digest
SHA256 2801190c37f96763c0e9b4f145ecf21faeeb4c8f48c5cf595eefe3f391df8d7b
MD5 359219194e76628d2dea0fbfb6b4a10f
BLAKE2b-256 db438d7986d2e7bf952cc0d3721625dab02a66a5d7acd414882de45c79dbe270

See more details on using hashes here.

File details

Details for the file simple_threaded_tcp-1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_threaded_tcp-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cf258154026f4922d646ca293ef3be1f7c7ed3bf89e960948400a91b5bf2cf7f
MD5 2dc10b2f4ea156b5eb4260bae98a8089
BLAKE2b-256 0ab899049d56add9259274b078d3125868ad33652df3ea97234dc6f41f32b208

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