phpPOST 数据到 Python - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lanqing
V2EX    Python

phpPOST 数据到 Python

  •  
  •   lanqing 2018-12-24 16:46:46 +08:00 3214 次点击
    这是一个创建于 2486 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我用 python 语言

    postdata = {'pic':[1,2,3]} requests.post(url,data=postdata) 

    在后台可以看到这样的数据 pic:[1,2,3]

    但是 php 他们传给我的时候(我不知道他们代码怎么写的,他们说只能这样传) 我接受这样的数据 pic[0]:1,pic[1]:2,pic[2]:3

    我想问 php 代码该怎么写,我再后台能得到 pic:[1,2,3]这样的数据

    23 条回复    2018-12-26 08:55:25 +08:00
    meik2333
        1
    meik2333  
       2018-12-24 16:58:33 +08:00
    首先你这个标题起的。。。

    其次其实 PHP 传的是对的,Python 传的反而有问题。

    如果你想要让 PHP 也传这样的数据,让他们拼接成字符串传过来吧。
    lanqing
        2
    lanqing  
    OP
       2018-12-24 17:01:55 +08:00
    @meik2333 语言组织能力不行...
    django 服务器接收请求
    为啥说 python 传的有问题, python 传的在网络中传输的就是 pic=1;pic=2;pic=3
    反而 php 在网络传输是 pic[0]=1;pic[1]=2;pic[2]=3
    如果拼成成字符串 那我接受的不就是字符串了....
    shuax
        3
    shuax  
       2018-12-24 17:02:09 +08:00 via Android
    直接传 json 吧
    mht data-uid=
        4
    mht  
       2018-12-24 17:03:59 +08:00
    干脆让 php 直接给你传完整的 json 好了 php 用 form-data 的话数组就是会带有下标的吧
    araraloren
        5
    araraloren  
       2018-12-24 17:04:27 +08:00
    交流要先统一格式。。
    lanqing
        6
    lanqing  
    OP
       2018-12-24 17:05:51 +08:00
    @mht
    @shuax
    其实传什么给我我都无所谓...解析一下就行 只是想知道 php 有没有这个功能能满足我的要求,没有就算了= =,
    markgor
        7
    markgor  
       2018-12-24 17:16:25 +08:00
    pic:[1,2,3] 这种格式真的没见过
    如果是 JSON 格式应该是:'{"pic":[1,2,3]}' 这样吧?
    PHP 传的 pic[0]:1,pic[1]:2,pic[2]:3 是典型的 form 形式提交给你,
    如果通过设置 JSON 方式提交给你,格式就是上面说到的'{"pic":[1,2,3]}'
    然后你通过 JSON 格式化下就能得到对应的值了( PY 没用过不清楚)。
    ninestep
        8
    ninestep  
       2018-12-24 17:16:40 +08:00
    1. 绝对能实现,这么个小需求说不能实现绝对是扯淡。
    2. 你 python 处理现在的数据成为你要的格式也绝对能实现,不能实现也是扯淡。
    3. 第一次在 post 请求中见到直接传语言的数据结构而不是 json,xml 等数据结构的
    lanqing
        9
    lanqing  
    OP
       2018-12-24 17:20:38 +08:00
    @markgor 你应该不知道 python 语言 pic:[1,2,3]=>标示 key:value , 然后 requests 库会自动转成相应的网络数据发送到服务器,完全没问题 . pic[0]:1,pic[1]:2,pic[2]:3 这个在我理解中就是三个 key:value key=>pic[0],pic[1],pic[2],如果这样传值 ,跟普通的 name:xxx,age:qqq 又有啥区别呢
    lanqing
        10
    lanqing  
    OP
       2018-12-24 17:22:42 +08:00
    @ninestep
    1. 你实现
    2. 我没说不能实现,只要规定了规则,你给我传什么,我都可以给你转回来
    3. 我用了 requests 库,没见过你可以查一下
    Vegetable
        11
    Vegetable  
       2018-12-24 17:23:07 +08:00
    1.你这个方式发的,传出去的是 form 格式,请求 body 是这样的 pic=1&pic=2&pic=3
    2.django 里接收的时候,应该是需要用 request.GET.getlist("pic")才能取到你说的值.
    3.php 发出来是什么样我不知道,但是你可以让他们拼接成 pic=1&pic=2&pic=3 这种形式发给你,但是也是不推荐的.还是建议使用 application/json 的形式发.
    lanqing
        12
    lanqing  
    OP
       2018-12-24 17:24:29 +08:00
    @Vegetable 你说的前两点对的,关于第三点,我个人觉得 php 实现不了,我才过来问的
    lanqing
        13
    lanqing  
    OP
       2018-12-24 17:29:25 +08:00
    我觉得我说的非常清楚了... 并不是我不会接收数据,我只想想问 php 有没有方法在 post 的时候 ,网络 body 是 pic=1&pic=2&pic=3 这样的, 这样我再 django 中就可以得到一个 list.
    我觉得我同事写的方式 post 数据是 pic[0]=1&pic[1]=2&pic[2]=3,这样我在 django 中得到的就是 pic[0],pic[1]分开的,而不是 list
    Vegetable
        14
    Vegetable  
       2018-12-24 17:34:34 +08:00
    @lanqing

    http://php.net/manual/en/function.curl-setopt.php

    CURLOPT_POSTFIELDS
    The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'.

    ***** This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' *****

    or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. The @ prefix can be disabled for safe passing of values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE.

    怎么会不行呢?我都不会写 PHP,也能找到怎么写啊
    lanqing
        15
    lanqing  
    OP
       2018-12-24 17:50:29 +08:00
    @Vegetable 修改请求头么.multipart/form-data 我试了下还是不行..
    markgor
        16
    markgor  
       2018-12-24 18:15:16 +08:00
    @lanqing
    Py 的我真的不清楚,

    但想确认一点,如果是 JSON 的数据的话,你是不是可以转为 LIST ?
    如果是的话,那 PHP 直接提交 JSON 给你就可以啦?
    我只试过 PHP 提交 JSON 去.NET 和 JAVA 都可以。

    附上 JSON 代码:
    <?php
    $arr = array(
    'pic'=>array(1,2,3,4)
    );
    $data_string = json_encode($arr);
    $ch = curl_init(你的地址);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string)
    ));

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
    print curl_error($ch);
    }
    curl_close($ch);
    echo $result;
    lanqing
        17
    lanqing  
    OP
       2018-12-24 20:23:17 +08:00
    @markgor json 肯定可以的
    learnshare
        18
    learnshare  
       2018-12-24 20:25:03 +08:00
    JSON 就好了
    Bryan0Z
        19
    Bryan0Z  
       2018-12-24 22:41:10 +08:00 via Android
    如果只是需要两种语言通信,可以考虑 GRPC
    xiangyuecn
        20
    xiangyuecn  
       2018-12-24 22:55:41 +08:00
    我觉得还是慎用数组形式的这种参数,简单字符串 key [唯一] 、字符串 value 多好,最怕这种数组形式的,对整个请求解析出来不是简单的字典也不是简单的数组,一个字烦,两个字麻烦,复杂的用 json 字符串 这个世界清静了
    bany
        21
    bany  
       2018-12-25 09:03:39 +08:00
    array(
    "pic" => array(1,2,3,4)
    )
    这不行?
    realpg
        22
    realpg  
    PRO
       2018-12-25 10:22:32 +08:00
    两头都比较坑系列

    成熟的 xml json 作为服务之间互相调用的基本套路是得到验证的
    a226679594
        23
    a226679594  
       2018-12-26 08:55:25 +08:00
    绝对能实现,这么个小需求说不能实现绝对是扯淡。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3861 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 10:19 PVG 18:19 LAX 03:19 JFK 06:19
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86