Execute curl command within a Python script

前端 未结 6 1560
轻奢々
轻奢々 2020-12-02 06:42

I am trying to execute a curl command within a python script.

If I do it in the terminal, it looks like this:

curl -X POST -d  \'{\"nw_src\": \"10.0.         


        
6条回答
  •  长情又很酷
    2020-12-02 07:35

    Use this tool (hosted here for free) to convert your curl command to equivalent Python requests code:

    Example: This,

    curl 'https://www.example.com/' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Origin: https://www.example.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Cookie: SESSID=ABCDEF' --data-binary 'Pathfinder' --compressed
    

    Gets converted neatly to:

    import requests
    
    cookies = {
        'SESSID': 'ABCDEF',
    }
    
    headers = {
        'Connection': 'keep-alive',
        'Cache-Control': 'max-age=0',
        'Origin': 'https://www.example.com',
        'Accept-Encoding': 'gzip, deflate, br',
    }
    
    data = 'Pathfinder'
    
    response = requests.post('https://www.example.com/', headers=headers, cookies=cookies, data=data)
    

提交回复
热议问题