HELP urllib POST can't get it to work

Oleg Broytmann phd at phd.fep.ru
Fri Mar 30 05:43:50 EST 2001


On Fri, 30 Mar 2001, mike mcleod wrote:
> import urllib
> params = urllib.urlencode({'address': '07', 'identity': 'me',
>                            'resultpg':'html/sendresult.html', 'message':
> 'test',
>                            'fPage':'true',
>                            })
> f = urllib.urlopen("http://www.samemachine.uk/bin/afunc.pl?", params)
> print f.read()
>
> It looks like the order of the params come out different in the urlopen.

   This is because it uses dictionary, which may (and usually does) changes
the order of keys.

> But because the server will only accept in the order shown it fails.

   Wow! what a foolish server :)

> I have tried to do this using basic sockets but don't know how to get that
> work without lots of reading.

   I have a very little patch for urlencode that allows to pass lists, not
only dicts. My version is this:

# obj must be either dict or list of tuples [(k1, v1), (k2, v2), ...] or list of lists [[k1, v1], [k2, v2], ...]
def urlencode(obj):
     l = []
     if type(obj) == type({}):
         obj = obj.items()

     for k, v in obj:
         k = quote_plus(str(k))
         v = quote_plus(str(v))
         l.append(k + '=' + v)

     return string.join(l, '&')

Oleg.
----
     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.





More information about the Python-list mailing list