import hmac, base64, hashlib, urllib2
base = 'https://.......'
def makereq(key, secret, path, data):
    hash_data = path + chr(0) + data
    secret = base64.b64decode(secret)
    sha512 = hashlib.sha512
    hmac = str(hmac.new(secret, hash_data, sha512))
    header = {
        'User-Agent': 'My-First-test',
        'Rest-Key': key,
        'Rest-Sign': base64.b64encode(hmac),
        'Accept-encoding': 'GZIP',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    return urllib2.Request(base + path, data, header)
Error: File "C:/Python27/btctest.py", line 8, in makereq hmac = str(hmac.new(secret, hash_data, sha512)) UnboundLocalError: local variable 'hmac' referenced before assignment
Somebody knows why? Thanks
If you assign to a variable anywhere in a function, that variable will be treated as a local variable everywhere in that function. So you would see the same error with the following code:
foo = 2
def test():
    print foo
    foo = 3
In other words, you cannot access the global or external variable if there is a local variable in the function of the same name.
To fix this, just give your local variable hmac a different name:
def makereq(key, secret, path, data):
    hash_data = path + chr(0) + data
    secret = base64.b64decode(secret)
    sha512 = hashlib.sha512
    my_hmac = str(hmac.new(secret, hash_data, sha512))
    header = {
        'User-Agent': 'My-First-test',
        'Rest-Key': key,
        'Rest-Sign': base64.b64encode(my_hmac),
        'Accept-encoding': 'GZIP',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    return urllib2.Request(base + path, data, header)
Note that this behavior can be changed by using the global or nonlocal keywords, but it doesn't seem like you would want to use those in your case.
You are redefining the hmac variable within the function scope, so the global variable from the import statement isn't present within the function scope.  Renaming the function-scope hmac variable should fix your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With