如何将curl --data-binary转换为python请求?

时间:2018-07-03 00:26:19

标签: python curl python-requests

我有curl命令:

curl 'https://example.com/submit' \
    -H 'Accept: */*' \
    -H 'Referer: http://www.example.com/go/' \
    -H 'Origin: http://www.example.com' \
    -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36' \
    -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryFkQeCsmEXjVI5SJ0' \
    --data-binary $'------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\r\nContent-Disposition: form-data; name="firstName"\r\n\r\Oleg\r\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\r\nContent-Disposition: form-data; name="campaignCode"\r\n\r\n\r\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\r\nContent-Disposition: form-data; name="validation"\r\n\r\n{"firstName":"required"}\r\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--\r\n' \
    --compressed

具有:

--data-binary $'------WebKitFormBoundary...'

但是我转换为python代码的版本不起作用

import requests

headers = {
    'Accept': '*/*',
    'Referer': 'http://www.example.com/go',
    'Origin': 'http://www.example.com',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
    'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryFkQeCsmEXjVI5SJ0'
}

data = '$------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="firstName"\\r\\n\\r\\Oleg\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="campaignCode"\\r\\n\\r\\n\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="validation"\\r\\n\\r\\n{"firstName":"required"}\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--\\r\\n'

response = requests.post('https://example.com/submit', headers=headers,
                         data=data)

也许我缺少了什么?如何转换--data-binary $

curl命令返回:{"success":true,"count":1},但是pycode返回:{"success":false,"count":1}

还有错误:

  1. $字符串响应-data中没有{"success":false,"count":0}

  2. 使用bytes代替str的{​​{1}}。


更新

此curl命令在我的Linux上可与curl 7.58.0一起使用,但在Windows上,cmd命令返回的错误与没有data的pycode中的错误相同

2 个答案:

答案 0 :(得分:0)

“卷曲”版本的捕获数据包的有效载荷:

MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "----WebKitFormBoundaryFkQeCsmEXjVI5SJ0"
    [Type: multipart/form-data]
    First boundary: ------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="firstName"\r\n
        Data (6 bytes)
            Data: 0d5c4f6c6567
            [Length: 6]
    Boundary: \r\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="campaignCode"\r\n\r\n
    Boundary: \r\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="validation"\r\n\r\n
        Data (24 bytes)
            Data: 7b2266697273744e616d65223a227265717569726564227d
            [Length: 24]
    Last boundary: \r\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--\r\n

“ python”版本捕获的数据包的有效负载:

MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "----WebKitFormBoundaryFkQeCsmEXjVI5SJ0"
    [Type: multipart/form-data]
    Preamble: 242d2d2d2d2d2d5765624b6974466f726d426f756e646172...
    First boundary: ------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="campaignCode"\r\n\r\n
    Boundary: \r\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="validation"\r\n\r\n
        Data (24 bytes)
            Data: 7b2266697273744e616d65223a227265717569726564227d
            [Length: 24]
    Last boundary: \r\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--\r\n

第一个条目似乎无法正常识别。

您应该从python代码中的变量"$"的开头删除data

data = '------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="firstName"\\r\\n\\r\\Oleg\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="campaignCode"\\r\\n\\r\\n\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="validation"\\r\\n\\r\\n{"firstName":"required"}\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--\\r\\n'

然后,是“ python”版本的捕获数据包的有效载荷:

MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "----WebKitFormBoundaryFkQeCsmEXjVI5SJ0"
    [Type: multipart/form-data]
    First boundary: ------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="firstName"\r\n
        Data (6 bytes)
            Data: 0d5c4f6c6567
            [Length: 6]
    Boundary: \r\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="campaignCode"\r\n\r\n
    Boundary: \r\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="validation"\r\n\r\n
        Data (24 bytes)
            Data: 7b2266697273744e616d65223a227265717569726564227d
            [Length: 24]
    Last boundary: \r\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--\r\n

与“ curl”版本相同。

答案 1 :(得分:0)

问题是您的两个命令没有发送相同的数据。

假设您正在bash或类似的shell中运行此命令,$'-----blahblah…'并不意味着字符串'$-----blahblah…',它意味着环境变量'-----blahblah…'的内容(如果有) ,如果没有,则返回字符串'-----blahblah…'。由于几乎肯定没有该名称的变量,因此您只需要获取字符串即可,而无需使用前导$

但是在Python中,您将$作为数据的一部分。这意味着您在第一个多部分边界之前还有一个额外的条目$

要解决此问题,请不要这样做。代替这个:

data = '$------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="firstName"\\r\\n\\r\\Oleg\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="campaignCode"\\r\\n\\r\\n\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="validation"\\r\\n\\r\\n{"firstName":"required"}\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--\\r\\n'

…执行此操作:

data = '------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="firstName"\\r\\n\\r\\Oleg\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="campaignCode"\\r\\n\\r\\n\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0\\r\\nContent-Disposition: form-data; name="validation"\\r\\n\\r\\n{"firstName":"required"}\\r\\n------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--\\r\\n'