Created
March 5, 2025 05:25
-
-
Save ashwch/7076b68543498851dabee5050a1c4ec5 to your computer and use it in GitHub Desktop.
UV and PEP 723 example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /// script | |
| # requires-python = ">=3.11" | |
| # dependencies = [ | |
| # "requests<3", | |
| # "rich", | |
| # ] | |
| # /// | |
| import time | |
| import statistics | |
| import requests | |
| from rich.console import Console | |
| from rich.table import Table | |
| from datetime import datetime | |
| def ping_http(host, count=4): | |
| """Ping a host via HTTP and return the results""" | |
| results = [] | |
| url = f"https://{host}" | |
| console = Console() | |
| console.print(f"[bold blue]Pinging {host} {count} times...[/bold blue]") | |
| for i in range(count): | |
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| try: | |
| start_time = time.time() | |
| response = requests.get(url, timeout=3) | |
| elapsed_ms = (time.time() - start_time) * 1000 | |
| if response.status_code < 400: | |
| status = "Success" | |
| else: | |
| status = f"HTTP {response.status_code}" | |
| except requests.exceptions.Timeout: | |
| status = "Timeout" | |
| elapsed_ms = None | |
| except requests.exceptions.ConnectionError: | |
| status = "Connection Error" | |
| elapsed_ms = None | |
| except requests.exceptions.RequestException: | |
| status = "Failed" | |
| elapsed_ms = None | |
| results.append({ | |
| "seq": i + 1, | |
| "timestamp": timestamp, | |
| "host": host, | |
| "status": status, | |
| "time_ms": round(elapsed_ms, 2) if elapsed_ms is not None else None | |
| }) | |
| # Display progress | |
| status_color = "green" if status == "Success" else "red" | |
| time_display = f"{elapsed_ms:.2f} ms" if elapsed_ms is not None else "N/A" | |
| console.print(f"Ping {i+1}: [{status_color}]{status}[/{status_color}] - {time_display}") | |
| # Wait a bit between requests | |
| if i < count - 1: | |
| time.sleep(1) | |
| return results | |
| def display_results_table(results): | |
| """Display ping results in a colorful table""" | |
| console = Console() | |
| table = Table(title=f"HTTP Ping Results for {results[0]['host']}") | |
| table.add_column("#", style="cyan", justify="right") | |
| table.add_column("Timestamp", style="magenta") | |
| table.add_column("Host", style="blue") | |
| table.add_column("Status", style="bold") | |
| table.add_column("Response Time", style="green", justify="right") | |
| # Track successful pings for statistics | |
| successful_times = [] | |
| for result in results: | |
| if result["status"] == "Success": | |
| status_style = "green" | |
| if result["time_ms"] is not None: | |
| successful_times.append(result["time_ms"]) | |
| elif result["status"].startswith("HTTP"): | |
| status_style = "yellow" | |
| else: | |
| status_style = "red" | |
| time_value = f"{result['time_ms']} ms" if result["time_ms"] is not None else "N/A" | |
| table.add_row( | |
| str(result["seq"]), | |
| result["timestamp"], | |
| result["host"], | |
| f"[{status_style}]{result['status']}[/{status_style}]", | |
| time_value | |
| ) | |
| console.print(table) | |
| # Print summary statistics | |
| console.print("\n[bold]Summary Statistics:[/bold]") | |
| if successful_times: | |
| console.print(f" [green]Successful requests:[/green] {len(successful_times)}/{len(results)}") | |
| console.print(f" [cyan]Min/Avg/Max:[/cyan] {min(successful_times):.2f}/{statistics.mean(successful_times):.2f}/{max(successful_times):.2f} ms") | |
| if len(successful_times) > 1: | |
| console.print(f" [cyan]Standard Deviation:[/cyan] {statistics.stdev(successful_times):.2f} ms") | |
| else: | |
| console.print(f" [red]All requests failed[/red]") | |
| def main(): | |
| host = "google.com" | |
| count = 5 | |
| results = ping_http(host, count) | |
| print() # Add a blank line for better readability | |
| display_results_table(results) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment