On Thu, May 29, 2008 at 3:54 AM, John M <[EMAIL PROTECTED]> wrote: > > I've run into some more trouble with what I think is an encoding > issue? > > I am writting a bittorrent tracker, and one of the GET parameters that > is passed is called info_hash, which is a lengthy / escaped hex > string, for example: > > GET /announce?info_hash=%EByXm%C5%7EmfD%D8%9A%91%D4%F7%C7%86%C7%D18%A7 > > the django / python code I am using to decode this is: > > request.encoding = "iso-8859-1" > hash = request.GET['info_hash'].encode('iso-8859-1').encode("hex") > > But when I print the hash field, i get this: > > efbfbd6defbfbd6d6644d89aefbfbdefbfbdc786efbfbd38efbfbd > > where it should be > > eb79586dc57e6d6644d89a91d4f7c786c7d138a7 > > I had it working for a while, but I'm not sure what changed to 'break' > this. > > Any help would be greatly appreciated >
First, the code you've included doesn't produce the answer you show, so I'm a little confused about what your code is really doing. In a python shell: >>> s = '%EByXm%C5%7EmfD%D8%9A%91%D4%F7%C7%86%C7%D18%A7' >>> s.encode('iso8859-1').encode('hex') '25454279586d2543352537456d664425443825394125393125443425463725433725383625433725443138254137' Where the 'efbbd...' is coming from I don't understand. But doing anything with an encoding like iso8859-1 isn't what you want to do anyway. You just want to un-do the percent-encoding and convert (encode) into the hex representation, using just the plain ASCII values for anything that wasn't percent-encoded: >>> urllib.unquote(s).encode('hex') 'eb79586dc57e6d6644d89a91d4f7c786c7d138a7' so try: import urllib hash = urllib.unquote(request.GET['info_hash']).encode('hex') Karen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---