[Tutor] urllib

Senthil Kumaran orsenthil at gmail.com
Mon Dec 7 09:10:53 CET 2009


On Mon, Dec 07, 2009 at 08:38:24AM +0100, Jojo Mwebaze wrote:
> I need help on something very small...
> 
> i am using urllib to write a query and what i want returned is 'FHI=128%2C128&
> FLO=1%2C1'
> 

The way to use urllib.encode is like this:

>>> urllib.urlencode({"key":"value"})
'key=value'
>>> urllib.urlencode({"key":"value","key2":"value2"})
'key2=value2&key=value'

For your purpses, you need to construct the dict this way:

>>> urllib.urlencode({"FHI":'128,128',"FHO":'1,1'})
'FHO=1%2C1&FHI=128%2C128'
>>> 


And if you are to use variables, one way to do it would be:

>>> x1,y1,x2,y2 = 1,1,128,128
>>> fhi = str(x2) + ',' + str(y2)
>>> fho = str(x1) + ',' + str(y1)
>>> urllib.urlencode({"FHI":fhi,"FHO":fho})
'FHO=1%2C1&FHI=128%2C128'

-- 
Senthil


More information about the Tutor mailing list