Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Urllib.encode question

Hello HF so I have a quick question regarding connecting to Bittrex Api. I also apologize I'm very new to python, so sorry if this is an obvious fix.

So when I run the application and it trys to connect to my Api it throws this error on the encode function in urllib. I have installed the pip as well.

Code
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/bittrex/bittrex.py", line 64, in api_query
    request_url += urllib.urlencode(options)
AttributeError: module 'urllib' has no attribute 'urlencode'

[/code]
(Jul 30, 2017 11:22 AM)Peenuttt Wrote:  Hello HF so I have a quick question regarding connecting to Bittrex Api. I also apologize I'm very new to python, so sorry if this is an obvious fix.

So when I run the application and it trys to connect to my Api it throws this error on the encode function in urllib. I have installed the pip as well.

Code
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/bittrex/bittrex.py", line 64, in api_query
    request_url += urllib.urlencode(options)
AttributeError: module 'urllib' has no attribute 'urlencode'

[/code]

replace
PHP Code

<?php 
urllib
.urlencode 

with
PHP Code

<?php 
urllib
.parse.urlencode() 


next time post your code and do so with the PHP tags
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!
(Jul 30, 2017 11:27 AM)koolkid Wrote:  replace
PHP Code

<?php 
urllib
.urlencode 

with
PHP Code

<?php 
urllib
.parse.urlencode() 


next time post your code and do so with the PHP tags

Thanks for the help bro! But when I replaced that it threw another error somewhere else. Do you know how in Pycharm you can hold Command and click really anything in the src and it will take you to where it starts kinda? Lol, well when I go to do that on the encode function it says it "Can not find declaration" now shouldn't urllib's functions like encode be accessible when I installed it on pip?

PHP Code

<?php 
File 
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/hmac.py", line 42, in __init__
    raise TypeError
("key: expected bytes or bytearray, but got %r" % type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str' 
(Jul 30, 2017 11:35 AM)Peenuttt Wrote:  
PHP Code

<?php 
File 
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/hmac.py", line 42, in __init__
    raise TypeError
("key: expected bytes or bytearray, but got %r" % type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str' 

As the error says, you need bytes or bytearray not str. Post your script so we can find the error.
(Jul 30, 2017 12:46 PM)xorn.91 Wrote:  As the error says, you need bytes or bytearray not str. Post your script so we can find the error.
I'm using the package or source from the Bittrex import.
PHP Code

<?php 
#!/usr/bin/env python

# -*- coding: utf-8 -*-

from bittrex import Bittrex

APIKEY 
= "API"

SECRETKEY = "SECRET"

bittrex = Bittrex("APIKEY", "SECRETKEY")

steem_bittrex = bittrex.get_markets()

print(
steem_bittrex) 

When it threw the encode error it took me back to this in the Bittrex import library I guess.

PHP Code

<?php 
if not options:
            
options = {}
        
nonce = str(int(time.time() * 1000))
        
base_url = 'I cannot post links but it's the Bittrex Api from the website'
        request_url = ''

        if method in self.public_set:
            request_url = (base_url % '
public') + method + '?'
        elif method in self.market_set:
            request_url = (base_url % '
market') + method + '?apikey=' + self.api_key + "&nonce=" + nonce + '&'
        elif method in self.account_set:
            request_url = (base_url % '
account') + method + '?apikey=' + self.api_key + "&nonce=" + nonce + '&'

        request_url += urllib.urlencode(options)        # <---- This to be exact 

        signature = hmac.new(self.api_secret, request_url, hashlib.sha512).hexdigest()

        headers = {"apisign": signature}

        ret = requests.get(request_url, headers=headers)
        return ret.json() 

I thought it was something with urllib so I installed that in the pip and it still gave me the error. I'm thinking it maybe didn't install right or something because I'm pretty sure I'm using it right, but clearly not lmao. Thanks you guys for the help btw
What version of python are you using ???
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!
I'm guessing 3.5 I seem to have both on my computer but maybe Pycharm is using 3.5?

Okay so I changed the interpreter to 3.6 and reinstalled Bittrex and tried to install urllib but now I'm getting another error...

I need figure this shit outtt

PHP Code

<?php 
Collecting urllib
  Using cached urllib
-1.21.1.tar.gz
    Complete output from command python setup
.py egg_info:
    
Traceback (most recent call last):
      
File "<string>", line 1, in <module>
      
File "/private/var/folders/85/sk_w4t_x07qf6ps759ghw3s40000gn/T/pycharm-packaging/urllib/setup.py", line 191
        s
.connect((base64.b64decode(rip), 017620))
                                               ^
    
SyntaxError: invalid token
    
    
----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/85/sk_w4t_x07qf6ps759ghw3s40000gn/T/pycharm-packaging/urllib/ 
This is why I always tell people; fuck Pycharm. It unnecessarily complicates package and module installation.
Sublime + Ubuntu Terminal (Bash on Windows 10) is the best way to go imo. Dan Bader agrees.



urlencode your strings before/when substituting into request

replace all instances of this

PHP Code

<?php 
request_url 
+= urllib.urlencode(options) 

with this

PHP Code

<?php 
request_url 
+= urllib.parse.urlencode(options) 

that library is written in Python 2, and you're trying to use it on Python 3, thus the raised exceptions
(Jul 30, 2017 04:53 PM)icmp Wrote:  This is why I always tell people; fuck Pycharm. It unnecessarily complicates package and module installation.
Sublime + Ubuntu Terminal (Bash on Windows 10) is the best way to go imo. Dan Bader agrees.



urlencode your strings before/when substituting into request

replace all instances of this

PHP Code

<?php 
request_url 
+= urllib.urlencode(options) 

with this

PHP Code

<?php 
request_url 
+= urllib.parse.urlencode(options) 

that library is written in Python 2, and you're trying to use it on Python 3, thus the raised exceptions
Okay, so from that I had to use
PHP Code

<?php 
    from urllib
.parse import urlencode 

Then it threw this, does that parse function create a string?

PHP Code

<?php 
Traceback 
(most recent call last):
  
File "/Users/Pat/PycharmProjects/Data Charts/accessApi.py", line 13, in <module>
    
steem_bittrex = bittrex.get_markets()
  
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/bittrex/bittrex.py", line 80, in get_markets
    
return self.api_query('getmarkets')
  
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/bittrex/bittrex.py", line 65, in api_query
    signature 
= hmac.new(self.api_secret, request_url, hashlib.sha512).hexdigest()
  
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/hmac.py", line 144, in new
    return 
HMAC(key, msg, digestmod)
  
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/hmac.py", line 42, in __init__
    raise TypeError
("key: expected bytes or bytearray, but got %r" % type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str' 

It also gave me this info on the urlencode function

PHP Code

<?php 
"""Encode a dict or sequence of two-element tuples into a URL query string.

    If any values in the query arg are sequences and doseq is true, each
    sequence element is converted to a separate parameter.

    If the query arg is a sequence of two-element tuples, the order of the
    parameters in the output will match the order of parameters in the
    input.

    The components of a query arg may each be either a string or a bytes type.

    The safe, encoding, and errors parameters are passed down to the function
    specified by quote_via (encoding and errors only if a component is a str).
    """ 

But it says into a URL query string, so I'm stumped...

FINALLY, thank you guys very much! So after I did what you said with the parse function and switching to 2.7 it works fine now. On to parsing the information I get, appreciate the help guys.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by mayar2020Apr 13, 2020 08:52 PM 9 245 Apr 13, 2020 08:52 PM
Last Post: zataeta
[C#] by Fun VidosJan 27, 2020 02:36 AM 2 184 Jan 27, 2020 02:36 AM
Last Post: Fun Vidos
[Python] by N0TR3DAMUSMay 17, 2019 07:58 PM 8 143 May 17, 2019 07:58 PM
Last Post: demarco