mirror of
https://github.com/basicswap/basicswap.git
synced 2025-02-26 21:10:39 +00:00
use readURL / fix LINTING errors.
This commit is contained in:
parent
b6d29a33d2
commit
cdfb9132ad
4 changed files with 19 additions and 22 deletions
basicswap
|
@ -179,27 +179,20 @@ class BaseApp:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def readURL(self, url: str, timeout: int = 120, headers={}) -> bytes:
|
def readURL(self, url: str, timeout: int = 120, headers={}) -> bytes:
|
||||||
use_tor = self.is_tor_available()
|
open_handler = None
|
||||||
try:
|
if self.use_tor_proxy:
|
||||||
if use_tor:
|
open_handler = SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, self.tor_proxy_host, self.tor_proxy_port)
|
||||||
proxy_handler = SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, self.tor_proxy_host, self.tor_proxy_port)
|
opener = urllib.request.build_opener(open_handler) if self.use_tor_proxy else urllib.request.build_opener()
|
||||||
opener = urllib.request.build_opener(proxy_handler)
|
if headers is None:
|
||||||
else:
|
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
|
||||||
opener = urllib.request.build_opener()
|
request = urllib.request.Request(url, headers=headers)
|
||||||
|
return opener.open(request, timeout=timeout).read()
|
||||||
opener.addheaders = [(key, value) for key, value in headers.items()]
|
|
||||||
request = urllib.request.Request(url)
|
def logException(self, message) -> None:
|
||||||
|
self.log.error(message)
|
||||||
with opener.open(request, timeout=timeout) as response:
|
if self.debug:
|
||||||
return response.read()
|
self.log.error(traceback.format_exc())
|
||||||
except urllib.error.URLError as e:
|
|
||||||
if isinstance(e.reason, ConnectionRefusedError) and use_tor:
|
|
||||||
error_msg = f"Connection refused. Tor proxy might not be running. Error: {str(e)}"
|
|
||||||
else:
|
|
||||||
error_msg = f"URLError: {str(e)}"
|
|
||||||
except Exception as e:
|
|
||||||
error_msg = f"Unexpected error: {str(e)}"
|
|
||||||
return json.dumps({"Error": error_msg}).encode()
|
|
||||||
|
|
||||||
def torControl(self, query):
|
def torControl(self, query):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -7716,7 +7716,7 @@ class BasicSwap(BaseApp):
|
||||||
exchange_name_to = ci_to.getExchangeName('coingecko.com')
|
exchange_name_to = ci_to.getExchangeName('coingecko.com')
|
||||||
ticker_from = ci_from.chainparams()['ticker']
|
ticker_from = ci_from.chainparams()['ticker']
|
||||||
ticker_to = ci_to.chainparams()['ticker']
|
ticker_to = ci_to.chainparams()['ticker']
|
||||||
headers = {'Connection': 'close'}
|
headers = {'User-Agent': 'Mozilla/5.0', 'Connection': 'close'}
|
||||||
rv = {}
|
rv = {}
|
||||||
|
|
||||||
if rate_sources.get('coingecko.com', True):
|
if rate_sources.get('coingecko.com', True):
|
||||||
|
|
|
@ -49,6 +49,7 @@
|
||||||
<span id="cache-status" class="mr-4 text-sm text-gray-600 dark:text-gray-300"></span>
|
<span id="cache-status" class="mr-4 text-sm text-gray-600 dark:text-gray-300"></span>
|
||||||
<span id="tor-status" class="mr-4 text-sm {% if tor_established %}text-green-500{% else %}text-red-500{% endif %}">
|
<span id="tor-status" class="mr-4 text-sm {% if tor_established %}text-green-500{% else %}text-red-500{% endif %}">
|
||||||
Tor <> API: {% if tor_established %}Connected{% else %}Not Connected{% endif %}
|
Tor <> API: {% if tor_established %}Connected{% else %}Not Connected{% endif %}
|
||||||
|
<a href="https://academy.particl.io/en/latest/basicswap-guides/basicswapguides_tor.html" target="_blank" rel="noopener noreferrer" class="underline">(?)</a>
|
||||||
</span>
|
</span>
|
||||||
<div id="tooltip-volume" role="tooltip" class="inline-block absolute invisible z-10 py-2 px-3 text-sm font-medium text-white bg-blue-500 rounded-lg shadow-sm opacity-0 transition-opacity duration-300 tooltip">
|
<div id="tooltip-volume" role="tooltip" class="inline-block absolute invisible z-10 py-2 px-3 text-sm font-medium text-white bg-blue-500 rounded-lg shadow-sm opacity-0 transition-opacity duration-300 tooltip">
|
||||||
Toggle Coin Volume
|
Toggle Coin Volume
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
# Distributed under the MIT software license, see the accompanying
|
# Distributed under the MIT software license, see the accompanying
|
||||||
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
|
|
||||||
def extract_data(bytes_in):
|
def extract_data(bytes_in):
|
||||||
if bytes_in is None:
|
if bytes_in is None:
|
||||||
return None
|
return None
|
||||||
|
@ -16,10 +17,12 @@ def extract_data(bytes_in):
|
||||||
return None
|
return None
|
||||||
return str_in[start: end]
|
return str_in[start: end]
|
||||||
|
|
||||||
|
|
||||||
def get_tor_established_state(swap_client):
|
def get_tor_established_state(swap_client):
|
||||||
rv = swap_client.torControl('GETINFO status/circuit-established')
|
rv = swap_client.torControl('GETINFO status/circuit-established')
|
||||||
return extract_data(rv)
|
return extract_data(rv)
|
||||||
|
|
||||||
|
|
||||||
def page_tor(self, url_split, post_string):
|
def page_tor(self, url_split, post_string):
|
||||||
swap_client = self.server.swap_client
|
swap_client = self.server.swap_client
|
||||||
summary = swap_client.getSummary()
|
summary = swap_client.getSummary()
|
||||||
|
|
Loading…
Reference in a new issue