Merge pull request #193 from lbryio/check-remote-conn

Use lbry.io instead of google to check for internet connection
This commit is contained in:
Jack Robison 2016-10-19 15:06:33 -04:00 committed by GitHub
commit d4aa109b73
5 changed files with 38 additions and 47 deletions

View file

@ -1,16 +1,22 @@
import base64
import datetime
import distutils.version
import logging
import json
import random
import os
import json
import socket
import yaml
import datetime
from lbrynet.core.cryptoutils import get_lbry_hash_obj
blobhash_length = get_lbry_hash_obj().digest_size * 2 # digest_size is in bytes, and blob hashes are hex encoded
log = logging.getLogger(__name__)
def generate_id(num=None):
h = get_lbry_hash_obj()
if num is not None:
@ -81,4 +87,17 @@ def save_settings(path, settings):
def today():
return datetime.datetime.today()
return datetime.datetime.today()
def check_connection(server="www.lbry.io", port=80):
"""Attempts to open a socket to server:port and returns True if successful."""
try:
host = socket.gethostbyname(server)
s = socket.create_connection((host, port), 2)
return True
except Exception as ex:
log.info(
"Failed to connect to %s:%s. Maybe the internet connection is not working",
server, port, exc_info=True)
return False

View file

@ -5,7 +5,6 @@ import os
import platform
import random
import re
import socket
import string
import subprocess
import sys
@ -130,8 +129,6 @@ OK_CODE = 200
# TODO alert if your copy of a lbry file is out of date with the name record
REMOTE_SERVER = "www.google.com"
class Parameters(object):
def __init__(self, **kwargs):
@ -336,14 +333,6 @@ class Daemon(jsonrpc.JSONRPC):
self.set_wallet_attributes()
if os.name != 'nt':
# TODO: are we still using this?
lbrycrdd_path_conf = os.path.join(os.path.expanduser("~"), ".lbrycrddpath.conf")
if not os.path.isfile(lbrycrdd_path_conf):
f = open(lbrycrdd_path_conf, "w")
f.write(str(self.lbrycrdd_path))
f.close()
self.created_data_dir = False
if not os.path.exists(self.db_dir):
os.mkdir(self.db_dir)
@ -396,6 +385,13 @@ class Daemon(jsonrpc.JSONRPC):
self.wallet_dir = os.path.join(os.path.expanduser("~"), ".lbrycrd")
self.lbrycrd_conf = os.path.join(self.wallet_dir, "lbrycrd.conf")
self.wallet_conf = os.path.join(self.wallet_dir, "lbrycrd.conf")
if os.name != 'nt':
# TODO: are we still using this?
lbrycrdd_path_conf = os.path.join(os.path.expanduser("~"), ".lbrycrddpath.conf")
if not os.path.isfile(lbrycrdd_path_conf):
f = open(lbrycrdd_path_conf, "w")
f.write(str(self.lbrycrdd_path))
f.close()
def _responseFailed(self, err, call):
log.debug(err.getTraceback())
@ -600,13 +596,7 @@ class Daemon(jsonrpc.JSONRPC):
self._events = analytics.Events(context, base58.b58encode(self.lbryid), self._session_id)
def _check_network_connection(self):
try:
host = socket.gethostbyname(REMOTE_SERVER)
s = socket.create_connection((host, 80), 2)
self.connected_to_internet = True
except:
log.info("Internet connection not working")
self.connected_to_internet = False
self.connected_to_internet = utils.check_connection()
def _check_lbrynet_connection(self):
def _log_success():

View file

@ -4,7 +4,6 @@ import logging.handlers
import os
import webbrowser
import sys
import socket
from appdirs import user_data_dir
from twisted.web import server
@ -12,6 +11,7 @@ from twisted.internet import reactor, defer
from jsonrpc.proxy import JSONRPCProxy
from lbrynet.core import log_support
from lbrynet.core import utils
from lbrynet.lbrynet_daemon.DaemonServer import DaemonServer
from lbrynet.lbrynet_daemon.DaemonRequest import DaemonRequest
from lbrynet.conf import API_CONNECTION_STRING, API_INTERFACE, API_PORT, \
@ -30,19 +30,12 @@ lbrynet_log = os.path.join(log_dir, LOG_FILE_NAME)
log = logging.getLogger(__name__)
REMOTE_SERVER = "www.google.com"
if getattr(sys, 'frozen', False) and os.name == "nt":
os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(os.path.dirname(sys.executable), "cacert.pem")
def test_internet_connection():
try:
host = socket.gethostbyname(REMOTE_SERVER)
s = socket.create_connection((host, 80), 2)
return True
except:
return False
return utils.check_connection()
def stop():

View file

@ -31,22 +31,17 @@ from lbrynet.lbrynet_daemon.DaemonServer import DaemonServer
from lbrynet.lbrynet_daemon.DaemonRequest import DaemonRequest
from lbrynet.conf import API_PORT, API_INTERFACE, ICON_PATH, APP_NAME
from lbrynet.conf import UI_ADDRESS
from lbrynet.core import utils
if platform.mac_ver()[0] >= "10.10":
from LBRYNotify import LBRYNotify
log = logging.getLogger(__name__)
REMOTE_SERVER = "www.google.com"
def test_internet_connection():
try:
host = socket.gethostbyname(REMOTE_SERVER)
s = socket.create_connection((host, 80), 2)
return True
except:
return False
return utils.check_connection()
class LBRYDaemonApp(AppKit.NSApplication):

View file

@ -18,6 +18,7 @@ try:
except ImportError:
import win32gui
from lbrynet.core import utils
from lbrynet.lbrynet_daemon.DaemonServer import DaemonServer
from lbrynet.lbrynet_daemon.DaemonRequest import DaemonRequest
from lbrynet.conf import API_PORT, API_INTERFACE, ICON_PATH, APP_NAME
@ -36,16 +37,9 @@ log = logging.getLogger(__name__)
if getattr(sys, 'frozen', False) and os.name == "nt":
os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(os.path.dirname(sys.executable), "cacert.pem")
REMOTE_SERVER = "www.google.com"
def test_internet_connection():
try:
host = socket.gethostbyname(REMOTE_SERVER)
s = socket.create_connection((host, 80), 2)
return True
except:
return False
return utils.check_connection()
def non_string_iterable(obj):
@ -314,4 +308,4 @@ if __name__ == '__main__':
if start_daemon:
main(lbry_name)
else:
LBRYURIHandler.open_address(lbry_name)
LBRYURIHandler.open_address(lbry_name)