From fdc3cff1d64124882a16a4723c1de1ddf76d1093 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 20 May 2016 16:08:55 +0200 Subject: [PATCH] use requests instead of urllib mess --- trezorctl | 30 ++++++++++-------------------- trezorlib/tx_api.py | 17 ++++++----------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/trezorctl b/trezorctl index e52183552..29a30c229 100755 --- a/trezorctl +++ b/trezorctl @@ -4,8 +4,9 @@ import binascii import argparse import json import base64 -import urllib import tempfile +import requests +from io import BytesIO from trezorlib.client import TrezorClient, TrezorClientDebug @@ -211,12 +212,11 @@ class Commands(object): fp = open(args.file, 'r') elif args.url: print("Downloading from", args.url) - resp = urllib.urlretrieve(args.url) - fp = open(resp[0], 'r') - urllib.urlcleanup() # We still keep file pointer open + r = requests.get(args.url) + fp = r.content else: - resp = urllib.urlopen("https://mytrezor.com/data/firmware/releases.json") - releases = json.load(resp) + r = requests.get('https://mytrezor.com/data/firmware/releases.json') + releases = r.json() version = lambda r: r['version'] version_string = lambda r: ".".join(map(str, version(r))) if args.version: @@ -228,25 +228,15 @@ class Commands(object): args.url = release['url'] return self.firmware_update(args) - if fp.read(8) == '54525a52': - print("Converting firmware to binary") + if fp[:8] == b'54525a52': + fp = binascii.unhexlify(fp) - fp.seek(0) - fp_old = fp - - fp = tempfile.TemporaryFile() - fp.write(binascii.unhexlify(fp_old.read())) - - fp_old.close() - - fp.seek(0) - if fp.read(4) != 'TRZR': + if fp[:4] != b'TRZR': raise Exception("TREZOR firmware header expected") print("Please confirm action on device...") - fp.seek(0) - return self.client.firmware_update(fp=fp) + return self.client.firmware_update(fp=BytesIO(fp)) list.help = 'List connected TREZOR USB devices' ping.help = 'Send ping message' diff --git a/trezorlib/tx_api.py b/trezorlib/tx_api.py index 187a93a3c..c7d0832f0 100644 --- a/trezorlib/tx_api.py +++ b/trezorlib/tx_api.py @@ -3,19 +3,13 @@ import json from decimal import Decimal # from filecache import filecache, DAY from . import types_pb2 as proto_types - -try: - # For Python 3.0 and later - from urllib.request import urlopen -except ImportError: - # Fall back to Python 2's urllib2 - from urllib2 import urlopen +import requests def insight_tx(url, rawdata=False): if not rawdata: try: - f = urllib2.urlopen(url) - data = json.load(f) + r = requests.get(url, headers = {'User-agent': 'Mozilla/5.0'}) + data = r.json() except: raise Exception('URL error: %s' % url) else: @@ -49,8 +43,8 @@ def insight_tx(url, rawdata=False): def smartbit_tx(url, rawdata=False): if not rawdata: try: - f = urllib2.urlopen(url) - data = json.load(f) + r = requests.get(url, headers = {'User-agent': 'Mozilla/5.0'}) + data = r.json() except: raise Exception('URL error: %s' % url) else: @@ -90,6 +84,7 @@ class TXAPIBitcoin(object): url = 'https://insight.bitpay.com/api/tx/%s' % txhash return insight_tx(url) + class TXAPITestnet(object): # @filecache(DAY)