Skip to main content

Simple and secure TCP framework

Project description

GTCP

Simple and secure TCP framework

Features

  • TCP server and TCP client
  • Simple event based messaging
  • Secure automatic RSA encryption

Installation

Using pip:

$ pip install gtcp

In Python:

from gtcp import server, client

Documentation

Server

Initialization

The server comes as a class. When creating a server with the class, the constructor takes 1 required parameter: the port the server will run on.

from gtcp import server

# 's' is a TCP server that runs on port 8080
s = server(8080)

Additionally, the server also takes 1 optional parameter: options. Options should be passed as a dictionary.

from gtcp import server

# in this case, we are setting the "encrypted" option to true
s = server(8080, {"encrypted": True})

Handling connections

Server objects have the .connection() method to handle new client connections. It takes 1 parameter: a callback function with a parameter for the socket.

# The socket parameter will be set as an object that represents the connection
def connectionhandler(socket):
    pass
s.connect(connectionhandler)

The socket object is comprised of the IP address of the client, the socket id, the rooms the socket is in, methods for handling and sending data, and methods for handling rooms.

def connectionhandler(socket):
    print(socket.id)
    # Output: A random UUIDv4 id
    print(socket.ip)
    # Output: 0.0.0.0
    print(socket.rooms)
    # Output: [<the id of the socket>, rooms, the, socket, is, in...]
s.connect(connectionhandler)

Sending data

To send data, use the .emit() method. It takes at least two parameters: the event and any amount of data to be sent.

You can use the .emit() method with a server object or a socket object to send to all connected sockets or to a single socket respectively.

def connectionhandler(socket):
    # This will send to the specific socket from the parameter
    socket.emit("login", username, password)
    # This will send to all sockets
    s.emit("login", username, password)
s.connect(connectionhandler)

# This will send to all sockets
s.emit("login", username, password)

Recieving data

To recieve data from a socket, use it's .on() method. It takes two parameters: the event and a callback function with parameters for all the data.

def connectionhandler(socket):
    def loginhandler(username, password):
        pass
    socket.on("login", loginhandler)
s.connect(connectionhandler)

Rooms

To do certain interactions with specific sockets or to simple group and organize sockets, use rooms. Rooms are groups of sockets that you can adress seperately from others.

To get a socket to join a room, use the .join() method.

def connectionhandler(socket):
    socket.join("room1")
    print(socket.rooms)
    #  Output: [<the id of the socket>, "room1"]
s.connect(connectionhandler)

To get a socket to leave a room (excluding the room of their own id), use the .leave() method.

def connectionhandler(socket):
    socket.leave("room1")
    print(socket.rooms)
    # Output: [<the id of the socket>]
    socket.leave(socket.id)
    print(socket.rooms)
    # Output: [<the id of the socket>]
s.connect(connectionhandler)

To get a socket to leave all tooms except for the one is their own id, use the .clearrooms() method.

def connectionhandler(socket):
    socket.clearrooms()
    print(socket.rooms)
    # Output: [<the id of the socket>]
s.connect(connectionhandler)

To send data to a specific room, use server object's .to() method. It takes one parameter (the room). The .to() methods can be chained to send to multiple rooms.

def connectionhandler(socket):
    pass
socket.onconnection()

# This will emit to sockets in room1
socket.to("room1").emit("hello", "world")

# This will emit to both room1 and room2
socket.to("room1").to("room2").emit("hello", "world")

Handling client disconnect

To a socket disconnect, listen with the .on() method for a the "end" event. The callback function takes no parameters

def connectionhandler(socket):
    def endhandler():
        pass
    socket.on("end", endhandler)
s.connect(connectionhandler)

Client

Initialization

The client comes as a class. When creating a client with the class, the constructor takes 1 required parameters: the IP address and port of the server to connect to.

from gtcp import client

# 'c' is a TCP client connected to a server ran on port 8080 
c = client("localhost:8080")

Additionally it takes 2 more optional parameters: a callback function to run when the client connects to the server and an options dictionary.

from gtcp import client

# 'connectionhandler,' which is optional, is ran when it 'c' finishes connecting to the server
def clientcallback(c):
    pass

# 'c' is an encrypted TCP client connected to a server ran on port 8080
c = client("localhost:8080", {"encrypted": True}, clientcallback)

Sending data

To send data to the server, use the .emit() method. It takes at least two parameters: the event and any amount of data to be sent.

def clientcallback(c):
    # This will send to the server
    c.emit("login", username, password)

c = client("localhost:8080", clientcallback)

# This also sends to the server
c.emit("login", username, password)

Recieving data

To recieve data from the server, use the client object's .on() method. It takes two parameters: the event and a callback function with parameters for all the data.

def loginhandler(username, password):
    pass

def clientcallback(c):
    c.on("login", loginhandler)

c = client("localhost:8080", clientcallback)

# This also works
c.on("login", loginhandler)

Callbacks

Sometimes, it is useful to have a more traditional request-response style API. In GTCP, this is achieved with callback functions.

Callback functions are any functions that are sent through an .emit(). The function will be ran and supplied with parameters when it is called by the other side.

# In client
def clientcallback(c):
    def loginCallback(confirm, userToken=""):
        if confirm:
            print(userToken)
    c.emit("login", username, password, loginCallback)
c = client("localhost:8080", clientcallback)

# In server
def connectionhandler(socket):
    def loginhandler(username, password, callback):
        if db.exists("username", username):
            if db.where("username", username)["password"] == password:
                callback(1, db.where("username", username)["userToken"])
            else:
                callback(0)
        else:
            callback(0)
    socket.on("login", loginhandler)
s.connect(connectionhandler)

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

gtcp-0.3.7.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

gtcp-0.3.7-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file gtcp-0.3.7.tar.gz.

File metadata

  • Download URL: gtcp-0.3.7.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.9

File hashes

Hashes for gtcp-0.3.7.tar.gz
Algorithm Hash digest
SHA256 01c643e21a5a75ada6a94c168d8d471f681642a86501f82e58c454b018a0ebd7
MD5 cd7bb96bfecae11e5c5836de8fa4a681
BLAKE2b-256 8b7cc282a1c82f3acf815c6d1573272a48b03cf86e81f341abbe10c60f63ad37

See more details on using hashes here.

File details

Details for the file gtcp-0.3.7-py3-none-any.whl.

File metadata

  • Download URL: gtcp-0.3.7-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.9

File hashes

Hashes for gtcp-0.3.7-py3-none-any.whl
Algorithm Hash digest
SHA256 2fb41cdf5ca578e3c14f0ccfdae4df55ec41aa4b3548b4a3f68336e987663e4e
MD5 71fb2120b3d1986a59891367749caf04
BLAKE2b-256 1fdce61a18b595029dfdd16d0e27cc0f37039c436e26403355d79fb0a919d42d

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