Skip to content
Snippets Groups Projects
Commit 7b1a2069 authored by Sébastien DA ROCHA's avatar Sébastien DA ROCHA Committed by Moul
Browse files

[enh] add a 10 seconds timeout

parent c7675605
No related branches found
No related tags found
1 merge request!79#32 TLS certificate issues
...@@ -5,6 +5,7 @@ import socket ...@@ -5,6 +5,7 @@ import socket
import urllib.request import urllib.request
from sys import exit, stderr from sys import exit, stderr
CONNECTION_TIMEOUT = 10
def discover_peers(ep, discover): def discover_peers(ep, discover):
""" """
...@@ -107,7 +108,7 @@ def get_request(ep, path): ...@@ -107,7 +108,7 @@ def get_request(ep, path):
if ep["port"] == "443": if ep["port"] == "443":
url = "https://" + ep[address] + "/" + path url = "https://" + ep[address] + "/" + path
request = urllib.request.Request(url) request = urllib.request.Request(url)
response = urllib.request.urlopen(request) response = urllib.request.urlopen(request, timeout=CONNECTION_TIMEOUT)
encoding = response.info().get_content_charset('utf8') encoding = response.info().get_content_charset('utf8')
return loads(response.read().decode(encoding)) return loads(response.read().decode(encoding))
...@@ -119,7 +120,7 @@ def post_request(ep, path, postdata): ...@@ -119,7 +120,7 @@ def post_request(ep, path, postdata):
url = "http://" + ep[address] + ":" + ep["port"] + "/" + path url = "http://" + ep[address] + ":" + ep["port"] + "/" + path
if ep["port"] == "443": if ep["port"] == "443":
url = "https://" + ep[address] + "/" + path url = "https://" + ep[address] + "/" + path
request = urllib.request.Request(url, bytes(postdata, 'utf-8')) request = urllib.request.Request(url, bytes(postdata, 'utf-8'), timeout=CONNECTION_TIMEOUT)
try: try:
response = urllib.request.urlopen(request) response = urllib.request.urlopen(request)
except urllib.error.URLError as e: except urllib.error.URLError as e:
...@@ -131,6 +132,7 @@ def post_request(ep, path, postdata): ...@@ -131,6 +132,7 @@ def post_request(ep, path, postdata):
def best_node(ep, main): def best_node(ep, main):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(CONNECTION_TIMEOUT)
addresses, port = {"ip6", "ip4", "domain"}, int(ep["port"]) addresses, port = {"ip6", "ip4", "domain"}, int(ep["port"])
for address in addresses: for address in addresses:
if address in ep: if address in ep:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment