Correctly url encoding a user agent

escaping, python, string, urlencode

Solution

`urllib.urlencode` expects a mapping or sequence with two items each. as can be seen in the docs

In your code you would need to do the following:

urllib.urlencode({'Agent': UserAgent})

Problem

I'm new to Python and seem to be hitting a problem. I'm trying to urlencode a user agent string... ``` import urllib UserAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3 Gecko/2008092417 Firefox/3.0.3' print 'Agent: ' + UserAgent print urllib.urlencode(UserAgent) ``` Which results in... ``` Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3 Gecko/2008092417 Firefox/3.0.3 Traceback (most recent call last): File "D:\Source\SomePath\test.py", line 7, in <module> print urllib.urlencode(UserAgent) File "C:\Python26\lib\urllib.py", line 1254, in urlencode raise TypeError TypeError: not a valid non-string sequence or mapping object Press any key to continue . . . ``` I can only assume that although the `UserAgent` is being printed correctly, I'm either missing some string-escaping option on the way in or making a fundamental mistake regarding `urllib.urlencode()`?

Original source