在 Python 脚本中执行 curl 命令 [英] Execute curl command within a Python script

查看:121
本文介绍了在 Python 脚本中执行 curl 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 python 脚本中执行 curl 命令.

如果我在终端执行,它看起来像这样:

curl -X POST -d '{"nw_src": "10.0.0.1/32", "nw_dst": "10.0.0.2/32", "nw_proto": "ICMP", "actions":允许",优先级":10"}' http://localhost:8080/firewall/rules/0000000000000001

我看到了使用 pycurl 的建议,但我不知道如何将它应用到我的.

我尝试使用:

subprocess.call(['卷曲','-X','邮政','-d',流量_x,'http://localhost:8080/firewall/rules/0000000000000001'])

它有效,但有更好的方法吗?

解决方案

不要!

我知道,这就是答案";没有人想要.但如果某件事值得做,就值得做对,对吗?

这似乎是一个好主意,可能源于一种相当广泛的误解,即诸如 curl 之类的 shell 命令不是程序本身.

所以你要问的是我如何从我的程序中运行这个其他程序,只是为了发出一个微不足道的网络请求?".这太疯狂了,一定有更好的方法吧?

Uxio 的答案 确实有效.但它看起来很难Pythonic,是吗?仅仅为了一个小小的要求,就需要大量的工作.Python 应该是关于飞行!任何写作的人可能都希望他们只是call'd curl


<块引用>

它有效,但有更好的方法吗?

是的,有更好的方法!

请求:HTTP for Humans

<块引用>

事情不应该这样.不是在 Python 中.

让我们获取这个页面:

导入请求res = requests.get('https://stackoverflow.com/questions/26000336')

就是这样,真的!然后,您将获得原始 res.textres.json() 输出、res.headers

您可以查看文档(上面链接)以了解设置所有选项的详细信息,因为我认为 OP 现在已经发展了,而您 - 现在的读者 - 可能需要不同的.

但是,例如,它很简单:

url = 'http://example.tld'有效载荷 = { 'key' : 'val' }标题 = {}res = requests.post(url, data=payload, headers=headers)

您甚至可以使用一个很好的 Python dict 在带有 params={} 的 GET 请求中提供查询字符串.

简单而优雅.保持冷静,继续飞行.

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.0.1/32", "nw_dst": "10.0.0.2/32", "nw_proto": "ICMP", "actions": "ALLOW", "priority": "10"}' http://localhost:8080/firewall/rules/0000000000000001

I've seen recommendations to use pycurl, but I couldn't figure out how to apply it to mine.

I tried using:

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

and it works, but is there a better way?

解决方案

Don't!

I know, that's the "answer" nobody wants. But if something's worth doing, it's worth doing right, right?

This seeming like a good idea probably stems from a fairly wide misconception that shell commands such as curl are anything other than programs themselves.

So what you're asking is "how do I run this other program, from within my program, just to make a measly little web request?". That's crazy, there's got to be a better way right?

Uxio's answer works, sure. But it hardly looks very Pythonic, does it? That's a lot of work just for one little request. Python's supposed to be about flying! Anyone writing that is probably wishing they just call'd curl!


it works, but is there a better way?

Yes, there is a better way!

Requests: HTTP for Humans

Things shouldn’t be this way. Not in Python.

Let's GET this page:

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

That's it, really! You then have the raw res.text, or res.json() output, the res.headers, etc.

You can see the docs (linked above) for details of setting all the options, since I imagine OP has moved on by now, and you - the reader now - likely need different ones.

But, for example, it's as simple as:

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

You can even use a nice Python dict to supply the query string in a GET request with params={}.

Simple and elegant. Keep calm, and fly on.

这篇关于在 Python 脚本中执行 curl 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆