prowlpy icon indicating copy to clipboard operation
prowlpy copied to clipboard

body=urllib.urlencode(data)) error in Python 2.6

Open jdiederichs opened this issue 15 years ago • 1 comments

I got this error for a message that contained an apostrophy in the body (description): File "/usr/lib64/python2.6/urllib.py", line 1261, in urlencode v = quote_plus(str(v)) UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 42: ordinal not in range(128)

A workaround is to first convert the description to Unicode: data = { 'apikey': self.apikey, 'application': application, 'event': event, 'description': description.encode('utf-8'), 'priority': priority }

jdiederichs avatar Aug 05 '09 19:08 jdiederichs

This fix isn't recommended as it will break if you try and pass it an ascii string with out-of-position characters (done with above fix in place):

p.post(1,2,u'∆˙∂∆˚ƒ˙∆˚ß∂˙∆˚ƒ˙ß˚∂∆') True p.post(1,2,'∆˙∂∆˚ƒ˙∆˚ß∂˙∆˚ƒ˙ß˚∂∆') Traceback (most recent call last): File "", line 1, in File "prowlpy.py", line 39, in post 'description': description.encode('utf-8'), UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

It will also break if you pass it a non-string type (urlencode does the type casting right now)

A work around for now would be to do the encoding before you pass the variable into function (quick scripts are likely to use non-unicode strings, whereas trying to port this into something like Django would probably use the unicode type).

That said, better UTF8 support that just works should become a priority, and I'll add it to the list and see what similar python libraries behave.

jacobb avatar Aug 05 '09 23:08 jacobb