A TUI-based network utility for TCP/UDP listen/write operations.
Project description
Python Network Utility Script with Curses TUI
This project provides a simple Text User Interface (TUI) based network utility written in Python. It allows you to either listen on a specified TCP or UDP port for incoming data or write (send) data to a remote TCP/UDP endpoint. All interactions, including setting up the connection parameters and sending messages, are handled directly within the terminal interface.
Features
-
TUI (Text User Interface): Interactive command-line interface using the
curseslibrary for a more engaging user experience. -
TCP and UDP Support: Supports both TCP (Transmission Control Protocol) for connection-oriented communication and UDP (User Datagram Protocol) for connectionless datagrams.
-
Listen Mode: Act as a server to receive data on a specified IP address and port.
-
Write Mode: Act as a client to send data to a specified IP address and port.
-
Dynamic Configuration: All network parameters (IP, Port, Protocol, Mode, Display Format) are prompted for within the TUI at startup.
-
Real-time Display: Shows incoming and outgoing messages in a dedicated output area.
-
Configurable Display Format: Choose to view received data in either human-readable ASCII or detailed hexadecimal dump format.
-
Responsive Input: Designed to allow smooth typing even while receiving concurrent data.
-
Local IP Auto-detection: Automatically suggests your local IP address as a default for convenience.
Requirements
-
Python 3.x
-
A Unix-like operating system (Linux, macOS) for native
cursessupport.
How to Run
-
Save the script: Save the provided Python code into a file named
network_utility_tui.py(or any other.pyname). -
Open your terminal or command prompt.
-
Navigate to the directory where you saved the script.
-
Run the script:
python network_utility_tui.py
The TUI will start, and you will be prompted to enter the required network parameters:
-
IP address to listen on: The IP address to bind to for listening (e.g.,
127.0.0.1for localhost,0.0.0.0to listen on all available interfaces). -
Port number: The port number to use (e.g.,
8080,12345). -
Protocol:
tcporudp. -
Mode:
listenorwrite. -
Display received data as:
asciiorhex.
Usage Examples
Once the TUI starts, follow the prompts.
Example 1: Listening on a TCP Port
-
Run:
python network_utility_tui.py -
Enter
0.0.0.0for IP (or your local IP). -
Enter
8080for Port. -
Enter
tcpfor Protocol. -
Enter
listenfor Mode. -
Enter
ascii(orhex) for Display Format.
The utility will start listening. You can then use another tool (like netcat, telnet, or a simple client script) to send data to your machine's IP address on port 8080.
Example using netcat from another terminal:
echo "Hello from client!" | nc 127.0.0.1 8080
(Replace 127.0.0.1 with the IP address you entered if listening on a different interface).
Example 2: Sending Messages via UDP
-
Run:
python network_utility_tui.py -
Enter
127.0.0.1for IP (or the target IP). -
Enter
12345for Port. -
Enter
udpfor Protocol. -
Enter
writefor Mode. -
Enter
ascii(orhex) for Display Format.
The utility will connect (or prepare to send for UDP). You can then type messages at the >> prompt and press Enter to send them. Any received data will also be displayed.
Troubleshooting
-
_curses.error: addwstr() returned ERRor_curses.error: embedded null character: This error typically occurs whencursesattempts to display a string containing a null character (\x00). The script now includes a.replace('\x00', '.')to mitigate this for ASCII display. If it persists, ensure the data you are sending does not contain unprintable characters that might cause rendering issues. -
curses.error: setupterm: could not find terminal: This means your terminal environment doesn't havecursesproperly configured. Ensure you are running in a compatible terminal (e.g.,xterm,gnome-terminal,iTerm2). On Windows, ensurewindows-cursesis installed and you're using a compatible terminal likecmd.exeor PowerShell. -
Cannot type in "write" mode: This issue has been addressed by changing the input handling to a non-blocking character-by-character method. Ensure you are running the latest version of the script. If still unresponsive, check for very high CPU usage or other processes interfering with terminal input.
-
"Address already in use" error: This means another process is already using the specified IP address and port. Choose a different port, or ensure no other applications are running on that port.
-
"Connection refused" error: In
writemode, this means the target IP/port does not have a listening server. Ensure the server application is running and accessible. -
Exiting the TUI: Press
Ctrl+Cat any time to gracefully exit the application.
How it Works (Technical Overview)
The script leverages Python's socket module for network communication and the curses library for building the TUI.
-
curses.wrapper: Initializes and deinitializes thecursesenvironment safely. -
Windows (
output_window,input_window): The screen is divided into two main areas:output_windowfor displaying logs and received data, andinput_windowfor user input. -
Threading: Network operations (listening for connections, receiving data) run in separate
threading.Threadinstances. This prevents the TUI from freezing while waiting for network events. -
curses_lock: Athreading.Lockis used to ensure that only one thread attempts to update thecursesscreen at a time, preventing display corruption. -
Non-blocking Input (
input_window.nodelay(True),curses.noecho()): For the "write" mode, user input is captured character by character in a non-blocking manner. This allows the main UI loop to remain responsive and redraw the input line as you type, even when network data is coming in. -
Data Formatting: Received raw bytes are either decoded to UTF-8 (with
errors='backslashreplace'to show unprintable characters as\xNNescapes) for ASCII display or converted to a formatted hexadecimal string for hex dump display.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file netutil-tui-0.1.2.tar.gz.
File metadata
- Download URL: netutil-tui-0.1.2.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2d869a4960e9aab0387a390bfb703a8e470dba99370d618a7dca124e81bf76f
|
|
| MD5 |
d71f24b16e2f243a63c4a235d18adb4f
|
|
| BLAKE2b-256 |
f56a16cfff4ade8b0d2e003e7345cba86b6899f84571fbd257ffc692a44920a0
|
File details
Details for the file netutil_tui-0.1.2-py3-none-any.whl.
File metadata
- Download URL: netutil_tui-0.1.2-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c722dc3fc13506cd4da24d9de20a5aac5881d00910c3bb9a26049370862ba969
|
|
| MD5 |
aff6907e8d2d6bba1760663020cb141b
|
|
| BLAKE2b-256 |
22aa56f3bb8bb604f520840ec94769c79bfdd5c6c9ade87054110673964fcab3
|