如何在 post 查询中传递 python 列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/349369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:56:17  来源:igfitidea点击:

How do I pass a python list in the post query?

pythonweb-services

提问by Mohit Ranka

I want to send some strings in a list in a POST call. eg:

我想在 POST 调用中的列表中发送一些字符串。例如:

    www.example.com/?post_data = A list of strings

The python code receives the data as a single string (Instead of a list of strings). How do I post it as a list of strings?

python 代码将数据作为单个字符串(而不是字符串列表)接收。如何将其发布为字符串列表?

采纳答案by Jon Skeet

There's no such thing as a "list of strings" in a URL (or in practically anything in HTTP - if you specify multiple values for the same header, they come out as a single delimited value in most web app frameworks IME). It's just a single string. I suggest you delimit the strings in some way (e.g. comma-separated) and then parse them out again at the other end.

URL 中没有“字符串列表”(或几乎在 HTTP 中的任何内容 - 如果您为同一个标头指定多个值,它们在大多数 Web 应用程序框架 IME 中作为单个分隔值出现)。它只是一个字符串。我建议您以某种方式分隔字符串(例如逗号分隔),然后在另一端再次解析它们。

回答by yrcjaya

TRY JSON(JavaScript Object Notation) it's available in the python package. Find out here: http://docs.python.org/library/json.html

尝试 JSON(JavaScript Object Notation) 它在 python 包中可用。在这里找到:http: //docs.python.org/library/json.html

You can Encode your list to an array represented in JSON and append to the post argument. Later decode it back to list...

您可以将列表编码为以 JSON 表示的数组并附加到 post 参数。稍后将其解码回列表...

回答by do3cc

It depends on your server to format the incoming arguments. for example, when zope gets a request like this: http://www.zope.org?ids:list=1&ids:list=2

这取决于您的服务器来格式化传入的参数。例如,当 zope 收到这样的请求时:http://www.zope.org?ids:list=1&ids:list =2

you can get the the ids as a list. But this feature depends on the server. If your server does not support some kind of parsing and validating your input, you have to implement it by yourself. Or you use zope.

您可以将 ID 作为列表获取。但此功能取决于服务器。如果您的服务器不支持某种解析和验证您的输入,您必须自己实现它。或者你使用zope。

回答by mepcotterell

If the big string you're receiving is merely delimited then you could try splitting it. See Splitting strings.

如果您收到的大字符串只是分隔符,那么您可以尝试拆分它。请参阅拆分字符串

To clarify, you get the delimited list of the strings, split that list into a python list, and voila!, you have a python list...

澄清一下,您获得了字符串的分隔列表,将该列表拆分为一个 python 列表,瞧!,您有一个 python 列表...

回答by S.Lott

Are you talking about this?

你在说这个吗?

post_data= ",".join( list_of_strings )

回答by Joelbitar

If you can't or don't want to simply separate them with a comma and you want to send them in a more list-ish way. I have a list of numbers that I want to pass and I use a PHP webservice on the other end, I don't want to rebuild my webservice since I'v used a common multiselect element that Zend Framework provided.

如果您不能或不想简单地用逗号分隔它们,并且您想以更列表的方式发送它们。我有一个我想传递的数字列表,我在另一端使用 PHP Web 服务,我不想重建我的 Web 服务,因为我使用了 Zend Framework 提供的通用多选元素。

This example works fine for me and my little integers and it would with your strings, I actualy don't perform the urllib.quote(s), I just do a str(s).

这个例子对我和我的小整数都很好,它也适用于你的字符串,我实际上不执行 urllib.quote(s),我只是做一个 str(s)。

Import urllib

导入urllib

import urllib

Your list of stings:

你的蜇伤清单:

string_list = ['A', 'list', 'of', 'strings', 'and', '?thér', '.&st,u?ff,']

Join together the list of strings with 'post_data[]=', also urlencode the string

将字符串列表与 'post_data[]=' 连接在一起,也对字符串进行 urlencode

post_data = '&'.join('post_data[]='+urllib.quote(s) for s in string_list)

Posts to http://example.com/

发布到http://example.com/

urllib.urlopen('http://example.com/',post_data)

回答by Philippe F

Data passed to a POST statement is (as far as I understood) encoded as key-value pairs, using the application/x-www-form-urlencoded encoding.

传递给 POST 语句的数据(据我所知)使用 application/x-www-form-urlencoded 编码编码为键值对。

So, I'll assume that you represent your list of string as the following dictionnary :

因此,我假设您将字符串列表表示为以下字典:

>>> my_string_list= { 's1': 'I',                                                
...     's2': 'love',                                                           
...     's3': 'python'                                                          
... }                   

Then, passing it as argument to POST is as difficult as reading the documentation of urllib.

然后,将它作为参数传递给 POST 就像阅读 urllib 的文档一样困难。

>>> import urllib
>>> print urllib.urlopen( 'http://www.google.fr/search', 
       urllib.urlencode( my_string_list ) 
    ).read()

Note that google does not use POST for its search queries, but you will see the error reported by google.

请注意,google 不会将 POST 用于其搜索查询,但您会看到 google 报告的错误。

If you run WireShark while typing the code above, you will see the data of the POST being passed as :

如果您在键入上面的代码时运行 WireShark,您将看到 POST 的数据被传递为:

 s3=python&s2=love&s1=I

回答by muhuk

A data structure like django.utils.datastructures.MultiValueDictis a clean way to represent such data. AFAIK it preserves order.

类似的数据结构django.utils.datastructures.MultiValueDict是表示此类数据的一种简洁方式。AFAIK 它保留了秩序。

>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
>>> d['name']
'Simon'
>>> d.getlist('name')
['Adrian', 'Simon']
>>> d.get('lastname', 'nonexistent')
'nonexistent'
>>> d.setlist('lastname', ['Holovaty', 'Willison'])

Django is using django.http.QueryDict(subclass of MultiValueDict) to turn a query string into python primitives and back.

Django 正在使用django.http.QueryDict(的子类MultiValueDict)将查询字符串转换为 Python 原语并返回。

from django.http import QueryDict

qs = 'post_data=a&post_data=b&post_data=c'

query_dict = QueryDict(qs)

assert query_dict['post_data'] == 'c'
assert query_dict.getlist('post_data') == ['a', 'b', 'c']
assert query_dict.urlencode() == qs

You should be able to copy these classes and use them in your project. (I haven't checked all dependencies though)

您应该能够复制这些类并在您的项目中使用它们。(虽然我还没有检查所有依赖项)