Skip to main content

Network Package

This package provides a client-server communication system using TCP and UDP With SSL/TLS security Certification.

Installation

To install the package, run:

pip install network_com

Generating SSL Certificates

To use SSL/TLS with this package, you need to generate SSL certificates. For development purposes, you can create self-signed certificates using OpenSSL:

Generate a Private Key

openssl genpkey -algorithm RSA -out server.key

Generate a Certificate Signing Request (CSR)

openssl req -new -key server.key -out server.csr -subj "/CN=example.com"

###Generate a Self-Signed Certificate

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

You will have server.key and server.crt files to be used by the server.

Using OpenSSL

Install OpenSSL

If you don't already have OpenSSL installed, you can install it:

On Ubuntu:

sudo apt-get install openssl

On macOS (using Homebrew):

brew install openssl

On Windows:

Download and install from OpenSSL for Windows.

Usage

Running the Server

To start the server, you can start the server using the package in your own script:

# my_server_script.py

from network_com import server
import threading
import os

def start_server():
    script_dir = os.path.dirname(os.path.abspath(__file__))
    udp_thread = threading.Thread(target=server.send_ip)
    tcp_thread = threading.Thread(target=server.tcp_server, args=(os.path.join(script_dir, 'server.crt'), os.path.join(script_dir, 'server.key')))
    udp_thread.start()
    tcp_thread.start()
    udp_thread.join()
    tcp_thread.join()

if __name__ == "__main__":
    start_server()

Run the script with:

python my_server_script.py

Running the Client

To start the client, you can start the client using the package in your own script:

# my_client_script.py

from network_com import client
import os

def start_client():
    script_dir = os.path.dirname(os.path.abspath(__file__))
    client.udp_listener(os.path.join(script_dir, 'server.crt'))

if __name__ == "__main__":
    start_client()

Run the script with:

python my_script.py

Combined Example

Here is an example script (combined_script.py) that allows you to run either the client or the server based on a command-line argument:

# combined_script.py

from network_com import client, server
import threading
import argparse

def start_client(certfile):
    client.udp_listener(certfile)

def start_server(certfile, keyfile):
    udp_thread = threading.Thread(target=server.send_ip)
    tcp_thread = threading.Thread(target=server.tcp_server, args=(certfile, keyfile))
    udp_thread.start()
    tcp_thread.start()
    udp_thread.join()
    tcp_thread.join()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Run network client or server.")
    parser.add_argument('mode', choices=['client', 'server'], help="Mode to run: client or server")
    parser.add_argument('--certfile', required=True, help="Path to the SSL certificate file")
    parser.add_argument('--keyfile', help="Path to the SSL key file (required for server)")

    args = parser.parse_args()

    if args.mode == 'client':
        start_client(args.certfile)
    elif args.mode == 'server':
        if not args.keyfile:
            parser.error("The --keyfile argument is required for running the server")
        start_server(args.certfile, args.keyfile)

Run the script with:

# To start the server
python combined_script.py server --certfile path/to/server.crt --keyfile path/to/server.key

# To start the client
python combined_script.py client --certfile path/to/server.crt

Using the Package in Other Projects

You can import and use the package in any other Python project. Here’s an example:

# another_script.py

from network_com import client, server
import threading

def main():
    # Start server in a separate thread
    server_thread = threading.Thread(target=start_server, args=(os.path.join(script_dir, 'server.crt'), os.path.join(script_dir, 'server.key')))
    server_thread.start()

    # Start client in the main thread (or another separate thread if desired)
    start_client("path/to/server.crt")

def start_server(certfile, keyfile):
    udp_thread = threading.Thread(target=server.send_ip)
    tcp_thread = threading.Thread(target=server.tcp_server, args=(certfile, keyfile))
    udp_thread.start()
    tcp_thread.start()
    udp_thread.join()
    tcp_thread.join()

def start_client(certfile):
    client.udp_listener(certfile)

if __name__ == "__main__":
    main()

Run the script with:

python another_script.py

Metadata

Release files for network-com 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for network-com 1.3.0
File Size Uploaded
network_com-1.3.0.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for network-com 1.3.0
File Interpreter ABI Platform
network_com-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.2 kB

Release files / network_com-1.3.0.tar.gz

Download URL network_com-1.3.0.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
700adaed2f19bb6cc2da2e7d9ffbe5ca497df8b38406d3f36eac385f8f455df0
BLAKE2b-256 checksum
How to use checksums
d71cb40e5baa98e4646a40c0e2da3cd4be149a82491effd71c68390e79cd57fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Release files / network_com-1.3.0-py3-none-any.whl

Download URL network_com-1.3.0-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a762a4dca4b3873b4f8cafbbe6d8e24981490d87098bb576a94156701141016
BLAKE2b-256 checksum
How to use checksums
d61391c63ec7df64731e3b50f77a39866965f8d49fbcfcc593389ef2954909e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.5

2 release files

1.2.3

2 release files

1.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page