1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-13 19:18:56 +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 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'

View File

@ -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)