Utoljára aktív 10 months ago
這段程式碼透過嘗試建立與 Google DNS 伺服器(8.8.8.8 的 53 埠)的 TCP 連線,來檢查網路是否正常連接,成功連線表示網路通暢,否則則視為網路有問題。
| 1 | import socket |
| 2 | |
| 3 | def is_connected(host='8.8.8.8', port=53, timeout=3): |
| 4 | """ |
| 5 | 嘗試連線到指定的 host 與 port,預設為 Google 的 DNS 伺服器。 |
| 6 | """ |
| 7 | try: |
| 8 | socket.setdefaulttimeout(timeout) |
| 9 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 10 | s.connect((host, port)) |
timmy / 台灣假日 API 查詢與快取
0 Kedvelések
0 forkok
1 fájlok
Utoljára aktív 10 months ago
這段程式碼是一個節假日查詢工具,從指定 API 獲取節假日資料,支援緩存與分頁,並提供按年份篩選節假日、檢查特定日期是否為節假日的功能,同時以 Holiday 類別封裝每個節假日的詳細資訊,便於操作與顯示。
| 1 | import requests |
| 2 | import os |
| 3 | import json |
| 4 | |
| 5 | class HolidayAPI: |
| 6 | def __init__(self, base_url, cache_dir="cache"): |
| 7 | """ |
| 8 | Initialize the HolidayAPI class. |
| 9 | :param base_url: The base URL for the API endpoint. |
| 10 | :param cache_dir: Directory to store cached data. |
Újabb
Régebbi