Ich habe viele ähnliche Fragen nachgeschlagen, aber leider ist keiner von ihnen in meiner Nähe.TypeError: Codierung ohne String-Argument
Ich habe ein einfaches Skript, das die Balance vom Austausch überprüft. Es ist ein Teil eines inoffiziellen API-Wrappers, der in Python geschrieben wurde, und mein Verständnis ist, dass es irgendwo zwischen Python 2 und Python 3 stecken bleibt. Ich habe Fehler nacheinander behoben, aber ich bin vollkommen bei diesem Problem. Hier ist der Code:
import urllib.parse
import urllib.request
import json
import time
import hmac,hashlib
class Poloniex():
def __init__(self, APIKey, Secret):
self.APIKey = APIKey
self.Secret = Secret
def api_query(self, command, req={}):
self.req = bytes(req, 'utf-8')
req['command'] = command
req['nonce'] = int(time.time()*1000)
post_data = urllib.parse.quote(req)
my_key = self.Secret
my_key_bytes = bytes(my_key, 'utf-8')
post_data_bytes = bytes(post_data, 'utf-8')
sign = hmac.new(my_key_bytes, post_data_bytes, hashlib.sha512).hexdigest()
headers = {
'Sign': sign,
'Key': my_key_bytes,
#'Content-Type': 'application/json'
}
ret = urllib.request.urlopen(
urllib.parse.quote('https://poloniex.com/tradingApi', safe=':/'), post_data_bytes,
headers)
jsonRet = json.loads(ret.read())
return self.post_process(jsonRet)
def returnBalances(self):
return self.api_query('returnBalances')
inst = Poloniex("AAA-BBB", "123abcdef")
balance = inst.returnBalances()
print(balance)
Sieht aus wie ich ein Problem mit der Syntax, sondern auch nach dem RTM kann ich das nicht herausfinden. Es wirft mich:
TypeError: encoding without a string argument
und davor hatte ich:
TypeError: quote_from_bytes() expected bytes
, die von
self.req = bytes(req, 'utf-8')
zeigen Kann jemand mir bitte in die richtige Richtung 'fixiert' worden ist?
Vielen Dank.
UPD
sorry, das Zurückverfolgungs vergessen
Traceback (most recent call last):
File "script.py", line 43, in <module>
balance = inst.returnBalances()
File "script.py", line 37, in returnBalances
return self.api_query('returnBalances')
File "script.py", line 18, in api_query
post_data = urllib.parse.quote(req)
File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/parse.py", line 775, in quote
return quote_from_bytes(string, safe)
File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/parse.py", line 800, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes
Welche der obigen Codezeilen löst den TypeError aus? – Gribouillis
oops, aktualisieren Sie die Frage – Colton