Skip to main content

A package for client-server communication over TCP and UDP

Project description

Network Package

This package provides a client-server communication system using TCP and UDP.

Installation

To install the package, run:

pip install network_com

Usage

Running the Server

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

# my_script.py

from network-com import server
import threading

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

if __name__ == "__main__":
    start_server()

Run the script with:

python my_script.py

Running the Client

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

# my_script.py

from network_com import client

def start_client():
    client.udp_listener()

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():
    client.udp_listener()

def start_server():
    udp_thread = threading.Thread(target=server.send_ip)
    tcp_thread = threading.Thread(target=server.tcp_server)
    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")

    args = parser.parse_args()

    if args.mode == 'client':
        start_client()
    elif args.mode == 'server':
        start_server()

Run the script with:

# To start the server
python combined_script.py server

# To start the client
python combined_script.py client

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)
    server_thread.start()

    # Start client in the main thread (or another separate thread if desired)
    start_client()

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

def start_client():
    client.udp_listener()

if __name__ == "__main__":
    main()

Run the script with:

python another_script.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

network_com-1.2.3rc2.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

network_com-1.2.3rc2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file network_com-1.2.3rc2.tar.gz.

File metadata

  • Download URL: network_com-1.2.3rc2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for network_com-1.2.3rc2.tar.gz
Algorithm Hash digest
SHA256 3282c21e1cf8b6f884d124e467c1369b8019008067d230528456276fb739ba75
MD5 bb228bb1ef46cf29669cf12fcbd202d2
BLAKE2b-256 356e40a799dd7837821736397411f1f47ca9ee00a46f0450664f1fe325a6b35d

See more details on using hashes here.

File details

Details for the file network_com-1.2.3rc2-py3-none-any.whl.

File metadata

File hashes

Hashes for network_com-1.2.3rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 c6907b0a86e47d295692abb22734a94b473edd7d3ad2c5e8770d9a9aaffcc971
MD5 9b34d1890217fa0dcf59ed426082f53b
BLAKE2b-256 e599abf9555f78214809fd62e87272c637a0985324855f69f9243dff8af02250

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page