在Python脚本中执行curl命令

nszi6y05  于 2022-11-13  发布在  Python
关注(0)|答案(8)|浏览(258)

我正在尝试在python脚本中执行curl命令。
如果我在终端中执行此操作,它看起来像这样:

curl -X POST -d  '{"nw_src": "10.0.0.1/32", "nw_dst": "10.0.0.2/32", "nw_proto": "ICMP", "actions": "ALLOW", "priority": "10"}' http://localhost:8080/firewall/rules/0000000000000001

我已经看到了使用pycurl的建议,但我不知道如何将其应用到我的。
我尝试使用:

subprocess.call([
    'curl',
    '-X',
    'POST',
    '-d',
    flow_x,
    'http://localhost:8080/firewall/rules/0000000000000001'
])

这是可行的,但有更好的方法吗

yqlxgs2m

yqlxgs2m1#

不要!
我知道,这是没有人想要的“答案”。但是,如果一件事值得去做,* 它就值得做对 *,对吗?
这看起来是个好主意,可能是因为人们普遍误解了curl之类的shell命令不是程序本身。
所以你问的是“我如何从我的程序中运行这个程序,只是为了发出一个微不足道的小Web请求?"这太疯狂了,一定有更好的方法,对吗?
Uxio's answer当然可以。但是它看起来不太像 * Python *,不是吗?仅仅为了一个小小的请求,就需要做很多工作。Python应该是关于flying的!任何写这篇文章的人可能都希望他们只是call 'd curl
这是可行的,但有更好的办法吗?
是的,有一个更好的方法!

Requests: HTTP for Humans(第一个字母)

事情不应该是这样的。在Python中不应该。
让我们来看看这个页面:

import requests
res = requests.get('https://stackoverflow.com/questions/26000336')

就是这样,真的!然后你就有了原始的res.text,或者res.json()输出,res.headers,等等。
你可以查看文档(上面的链接)来了解设置所有选项的细节,因为我想OP现在已经前进了,而你--现在的读者--可能需要不同的选项。
但是,举个例子,它就像这样简单:

url     = 'http://example.tld'
payload = { 'key' : 'val' }
headers = {}
res = requests.post(url, data=payload, headers=headers)

您甚至可以使用一个很好的Python dict在GET请求中提供params={}的查询字符串。
简单而优雅。保持冷静,继续飞翔。

7gs2gvoe

7gs2gvoe2#

使用这个tool(免费托管的here)将curl命令转换为等价的Python请求代码:
示例:

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

巧妙地转换为:

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)
xwmevbvl

xwmevbvl3#

你可以像@roippi说的那样使用urllib:

import urllib2
data = '{"nw_src": "10.0.0.1/32", "nw_dst": "10.0.0.2/32", "nw_proto": "ICMP", "actions": "ALLOW", "priority": "10"}'
url = 'http://localhost:8080/firewall/rules/0000000000000001'
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
for x in f:
    print(x)
f.close()
jgovgodb

jgovgodb4#

如果您没有过多地调整curl命令,也可以直接调用curl命令

import shlex
cmd = '''curl -X POST -d  '{"nw_src": "10.0.0.1/32", "nw_dst": "10.0.0.2/32", "nw_proto": "ICMP", "actions": "ALLOW", "priority": "10"}' http://localhost:8080/firewall/rules/0000000000000001'''
args = shlex.split(cmd)
process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
ktecyv1j

ktecyv1j5#

尝试使用子进程

CurlUrl="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"

使用getstatusoutput存储结果

status, output = subprocess.getstatusoutput(CurlUrl)
um6iljoc

um6iljoc6#

您可以使用以下代码片段

import shlex
import subprocess
import json

def call_curl(curl):
    args = shlex.split(curl)
    process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()
    return json.loads(stdout.decode('utf-8'))

if __name__ == '__main__':
    curl = '''curl - X
    POST - d
    '{"nw_src": "10.0.0.1/32", "nw_dst": "10.0.0.2/32", "nw_proto": "ICMP", "actions": "ALLOW", "priority": "10"}'
    http: // localhost: 8080 / firewall / rules / 0000000000000001 '''
    output = call_curl(curl)
    print(output)
yws3nbqq

yws3nbqq7#

改写这篇文章中的一个答案,而不是使用cmd.split().尝试用途:

import shlex

args = shlex.split(cmd)

然后将参数提供给subprocess.Popen。
查看此文档以了解更多信息:https://docs.python.org/2/library/subprocess.html#popen-constructor

s4n0splo

s4n0splo8#

subprocess模块中,还有一个名为run的选项使用它

from subprocess import run
run(curl -X POST -d  '{"nw_src": "10.0.0.1/32", "nw_dst": "10.0.0.2/32", "nw_proto": "ICMP", "actions": "ALLOW", "priority": "10"}' http://localhost:8080/firewall/rules/0000000000000001)

相关问题