Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Created February 25, 2025 22:59
Show Gist options
  • Save ThinGuy/d3b83b82f943fb8dda509aeaf5c19844 to your computer and use it in GitHub Desktop.
Save ThinGuy/d3b83b82f943fb8dda509aeaf5c19844 to your computer and use it in GitHub Desktop.
Protocol Checker
import requests
import ftplib
import subprocess
import socket
import argparse
import shutil
from urllib.parse import urlparse
def check_http_https(url):
"""Check if HTTP or HTTPS is available."""
try:
response = requests.get(url, timeout=5)
if response.status_code == 200:
return "Available"
else:
return f"Unavailable (HTTP(S) returned status: {response.status_code})"
except requests.RequestException as e:
return f"Unavailable ({str(e)})"
def check_ftp(server):
"""Check if FTP is available."""
try:
ftp = ftplib.FTP(server, timeout=5)
ftp.login() # Anonymous login test
ftp.quit()
return "Available"
except (socket.timeout, socket.gaierror) as e:
return f"Unavailable ({str(e)})"
except ftplib.error_perm:
return "Unavailable (Permission denied)"
except ftplib.error_temp:
return "Unavailable (Temporary server issue)"
except ftplib.error_proto:
return "Unavailable (Protocol error)"
except ftplib.error_reply:
return "Unavailable (Unexpected reply)"
except Exception as e:
return f"Unavailable ({str(e)})"
def check_rsync(server):
"""Check if rsync is available."""
try:
result = subprocess.run(["rsync", "--list-only", f"rsync://{server}/"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=5)
if result.returncode == 0:
return "Available"
else:
error_message = result.stderr.decode().strip()
return f"Unavailable (Error Code: {result.returncode}, Message: {error_message})"
except FileNotFoundError:
return "Unavailable (rsync not installed)"
except subprocess.TimeoutExpired:
return "Unavailable (Connection timed out)"
except Exception as e:
return f"Unavailable ({str(e)})"
def is_installed(command):
"""Check if a command-line utility is installed."""
return shutil.which(command) is not None
def display_http_utils(url):
"""Show available HTTP(S) download utilities that are installed."""
print("\nYou can download files using:")
utilities = {
"curl": f"curl -O {url}/filename",
"wget": f"wget {url}/filename",
"wget2": f"wget2 {url}/filename",
"aria2c": f"aria2c {url}/filename",
"axel": f"axel -n 10 {url}/filename"
}
for tool, command in utilities.items():
if is_installed(tool):
print(f" - {tool}: {command}")
def display_rsync_usage(server):
"""Show rsync commands for initial and incremental backups if rsync is installed."""
if is_installed("rsync"):
print("\nTo create an initial rsync backup, use:")
print(f" rsync -avz rsync://{server}/remote/path/ /local/backup/")
print("\nFor an incremental backup (only new/changed files), use:")
print(f" rsync -avzu rsync://{server}/remote/path/ /local/backup/")
def main():
parser = argparse.ArgumentParser(description="Check available protocols for mirroring/downloading files from a server.")
parser.add_argument("url", nargs="?", default="https://changelogs.ubuntu.com/changelogs", help="The URL of the server to check.")
args = parser.parse_args()
parsed_url = urlparse(args.url)
server_host = parsed_url.hostname or args.url # Extract host or use input as fallback
print(f"Checking available protocols for {args.url}...\n")
http_status = check_http_https(args.url)
ftp_status = check_ftp(server_host)
rsync_status = check_rsync(server_host)
print(f"HTTP(S): {http_status}")
print(f"FTP: {ftp_status}")
print(f"Rsync: {rsync_status}")
# Show HTTP utilities if HTTP(S) is available
if "Available" in http_status:
display_http_utils(args.url)
# Show rsync usage if Rsync is available
if "Available" in rsync_status:
display_rsync_usage(server_host)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment