1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-10 23:40:58 +00:00

use requests instead of urllib mess

This commit is contained in:
Pavol Rusnak 2016-05-20 16:08:55 +02:00
parent 61579deff6
commit fdc3cff1d6
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 16 additions and 31 deletions

View File

@ -4,8 +4,9 @@ import binascii
import argparse import argparse
import json import json
import base64 import base64
import urllib
import tempfile import tempfile
import requests
from io import BytesIO
from trezorlib.client import TrezorClient, TrezorClientDebug from trezorlib.client import TrezorClient, TrezorClientDebug
@ -211,12 +212,11 @@ class Commands(object):
fp = open(args.file, 'r') fp = open(args.file, 'r')
elif args.url: elif args.url:
print("Downloading from", args.url) print("Downloading from", args.url)
resp = urllib.urlretrieve(args.url) r = requests.get(args.url)
fp = open(resp[0], 'r') fp = r.content
urllib.urlcleanup() # We still keep file pointer open
else: else:
resp = urllib.urlopen("https://mytrezor.com/data/firmware/releases.json") r = requests.get('https://mytrezor.com/data/firmware/releases.json')
releases = json.load(resp) releases = r.json()
version = lambda r: r['version'] version = lambda r: r['version']
version_string = lambda r: ".".join(map(str, version(r))) version_string = lambda r: ".".join(map(str, version(r)))
if args.version: if args.version:
@ -228,25 +228,15 @@ class Commands(object):
args.url = release['url'] args.url = release['url']
return self.firmware_update(args) return self.firmware_update(args)
if fp.read(8) == '54525a52': if fp[:8] == b'54525a52':
print("Converting firmware to binary") fp = binascii.unhexlify(fp)
fp.seek(0) if fp[:4] != b'TRZR':
fp_old = fp
fp = tempfile.TemporaryFile()
fp.write(binascii.unhexlify(fp_old.read()))
fp_old.close()
fp.seek(0)
if fp.read(4) != 'TRZR':
raise Exception("TREZOR firmware header expected") raise Exception("TREZOR firmware header expected")
print("Please confirm action on device...") print("Please confirm action on device...")
fp.seek(0) return self.client.firmware_update(fp=BytesIO(fp))
return self.client.firmware_update(fp=fp)
list.help = 'List connected TREZOR USB devices' list.help = 'List connected TREZOR USB devices'
ping.help = 'Send ping message' ping.help = 'Send ping message'

View File

@ -3,19 +3,13 @@ import json
from decimal import Decimal from decimal import Decimal
# from filecache import filecache, DAY # from filecache import filecache, DAY
from . import types_pb2 as proto_types from . import types_pb2 as proto_types
import requests
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
def insight_tx(url, rawdata=False): def insight_tx(url, rawdata=False):
if not rawdata: if not rawdata:
try: try:
f = urllib2.urlopen(url) r = requests.get(url, headers = {'User-agent': 'Mozilla/5.0'})
data = json.load(f) data = r.json()
except: except:
raise Exception('URL error: %s' % url) raise Exception('URL error: %s' % url)
else: else:
@ -49,8 +43,8 @@ def insight_tx(url, rawdata=False):
def smartbit_tx(url, rawdata=False): def smartbit_tx(url, rawdata=False):
if not rawdata: if not rawdata:
try: try:
f = urllib2.urlopen(url) r = requests.get(url, headers = {'User-agent': 'Mozilla/5.0'})
data = json.load(f) data = r.json()
except: except:
raise Exception('URL error: %s' % url) raise Exception('URL error: %s' % url)
else: else:
@ -90,6 +84,7 @@ class TXAPIBitcoin(object):
url = 'https://insight.bitpay.com/api/tx/%s' % txhash url = 'https://insight.bitpay.com/api/tx/%s' % txhash
return insight_tx(url) return insight_tx(url)
class TXAPITestnet(object): class TXAPITestnet(object):
# @filecache(DAY) # @filecache(DAY)