mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Using threads to fetch from the IPmirrors
This commit is contained in:
parent
53941d7657
commit
f6695eb9c4
1 changed files with 11 additions and 2 deletions
|
@ -22,6 +22,7 @@ import os
|
||||||
import re
|
import re
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
import threading
|
||||||
|
|
||||||
from moulinette.utils.filesystem import read_file, write_to_file
|
from moulinette.utils.filesystem import read_file, write_to_file
|
||||||
from moulinette.utils.network import download_text
|
from moulinette.utils.network import download_text
|
||||||
|
@ -102,8 +103,7 @@ def get_public_ips(protocol=4):
|
||||||
ip_url_yunohost_tab = settings_get("security.ipmirrors.v"+str(protocol)).split(",")
|
ip_url_yunohost_tab = settings_get("security.ipmirrors.v"+str(protocol)).split(",")
|
||||||
ip_count = {} # Count the number of times an IP has appeared
|
ip_count = {} # Count the number of times an IP has appeared
|
||||||
|
|
||||||
# Check URLS
|
def thread_fun(ip_count,url):
|
||||||
for url in ip_url_yunohost_tab[:3]:
|
|
||||||
logger.debug(f"Fetching IP from {url}")
|
logger.debug(f"Fetching IP from {url}")
|
||||||
try:
|
try:
|
||||||
ip = download_text(url, timeout=15).strip()
|
ip = download_text(url, timeout=15).strip()
|
||||||
|
@ -116,6 +116,15 @@ def get_public_ips(protocol=4):
|
||||||
f"Could not get public IPv{protocol} from {url} : {e}"
|
f"Could not get public IPv{protocol} from {url} : {e}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Check URLS
|
||||||
|
threads = []
|
||||||
|
for url in ip_url_yunohost_tab[:3]: # Launch threads
|
||||||
|
thread = threading.Thread(target=thread_fun, args=(ip_count,url))
|
||||||
|
thread.start()
|
||||||
|
threads.append(thread)
|
||||||
|
for thread in threads: # Wait for the threads to finish
|
||||||
|
thread.join()
|
||||||
|
|
||||||
ip_list_with_count = [ (ip,ip_count[ip]) for ip in ip_count ]
|
ip_list_with_count = [ (ip,ip_count[ip]) for ip in ip_count ]
|
||||||
ip_list_with_count.sort(key=lambda x: x[1]) # Sort by frequency
|
ip_list_with_count.sort(key=lambda x: x[1]) # Sort by frequency
|
||||||
ips = [ x[0] for x in ip_list_with_count ]
|
ips = [ x[0] for x in ip_list_with_count ]
|
||||||
|
|
Loading…
Add table
Reference in a new issue