功能: 1. 抓取https://www.blocklist.de 網站提供的黑名單IP 1. 加上原本.htaccess既有的內容,寫入新的.htaccess 1. 設排程定期執行 目的: 禁止被列入黑名單的IP造訪網站 ![ccnet資料夾畫面](https://hackmd.io/_uploads/B18zSGQqyx.png) ![htaccess內容](https://hackmd.io/_uploads/r19fSMXcJx.png) windows設定排程,定期執行這支程式 /c "cd /d C:\XAMPP\htdocs && python wpblacklist.py" ![image](https://hackmd.io/_uploads/S1oZsfQqke.png) ```=py import os import requests def download_blocklist(url, save_path): try: response = requests.get(url, timeout=10) response.raise_for_status() ip_list = response.text.strip().split('\n') htaccess_content = """# BEGIN WordPress # 在含有 BEGIN WordPress 及 END WordPress 標記的這兩行間的指示詞內容為動態產生, # 且應僅有 WordPress 篩選器能進行修改。對這兩行間任何指示詞內容的變更, # 都會遭到系統覆寫。 <IfModule mod_rewrite.c> RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress Options All -Indexes <files ~ "^.*\.([Hh][Tt][Aa])"> order allow,deny deny from all satisfy all </files> <files wp-config.php> order allow,deny deny from all </files> # Limit logins and admin by IP <Limit GET POST PUT> order deny,allow """ for ip in ip_list: htaccess_content += f"deny from {ip}\n" htaccess_content += "</Limit>\n" os.makedirs(os.path.dirname(save_path), exist_ok=True) with open(save_path, "w", encoding="utf-8") as file: file.write(htaccess_content) print(f".htaccess file saved at: {save_path}") except requests.RequestException as e: print(f"Error downloading blocklist: {e}") if __name__ == "__main__": url = "https://lists.blocklist.de/lists/all.txt" save_path = r"C:\\CompareFolder\\.htaccess" download_blocklist(url, save_path) ```