开发者

Python urllib2 sending POST data

开发者 https://www.devze.com 2023-01-25 06:18 出处:网络
I am attempting to send POST data using urllib, but the problem is normally you urlencode your dictionary of fields and then you send that along with your request. But some of my fields that I have to

I am attempting to send POST data using urllib, but the problem is normally you urlencode your dictionary of fields and then you send that along with your request. But some of my fields that I have to send to the server are named the exact same name, so using a dictionary I end up deleting a lot of my data out of the dictionary because you can't have two keys with the same name and different data.

I have my request data in a string format in the correct syntax(eg. var=value&var=value2&...) and I try to send that along with my urllib request, but I get a bad request every 开发者_JAVA百科time. Is there any other way to urlencode the data before sending so I don't get a 400?


urllib.urlencode() can take a list of tuples, allowing you to specify multiple values for a parameter:

>>> urlencode([("var", "value"), ("var", "value2")])
'var=value&var=value2'

Or use a map with lists as values:

>>> p = {"var": ["value", "value2"], "var2": ["yetanothervalue"]}
>>> urlencode([(k, v) for k, vs in p.items() for v in vs])
'var=value&var=value2&var2=yetanothervalue'

You could even allow lists or strings as values, although it becomes less concise:

>>> p = {"var": ["value", "value2"], "var2": "yetanothervalue"}
>>> urlencode([(k, v) for k, vs in p.items()
...     for v in isinstance(vs, list) and vs or [vs]])
'var=value&var=value2&var2=yetanothervalue'
0

精彩评论

暂无评论...
验证码 换一张
取 消