如何使用 python post curl 命令

how to post a curl command using python

我需要这方面的帮助。基本上我需要使用这个命令。这是使用 CURL 给出的示例。我需要做的只是将其粘贴到 cmd 中,它会完成它的工作。

curl \
-H "Content-Type: application/json" -X POST \
-u "{username}":"{password}" \
-d "{\"dialog_node\":\"greeting\",\"conditions\":\"#hello\",\"output\":{\"text\":\"Hi! How can I help you?\"},\"title\":\"greeting\"}" "https://gateway-s.watsonplatform.net/conversation/api/v1/workspaces/bec28d8f-18c1-4e97-8d08-9c842c658b51/dialog_nodes?version=2017-05-26"

URL 文档可在此处找到: https://www.ibm.com/watson/developercloud/conversation/api/v1/?curl#create_dialognode

现在的问题是我想 运行 在 python 脚本中而不是在 CMD 中。我已经搜索 google 和 Whosebug 几个小时了..但我似乎找不到正确的答案..

到目前为止,我看到人们在使用 1.requests 2.urllib 3.urllib2 4.pycurl 5.subprocess

我想以正确的方式去做。在 python 脚本中 运行 上述命令的最佳方法是什么?我该怎么做?

我也在用 python 3

如果您使用的是 Watson Conversation,那么您可以只使用 Python WDC SDK。

https://github.com/watson-developer-cloud/python-sdk

对于上面的示例,它将是:

from watson_developer_cloud import ConversationV1

username = 'USERNAME',
password = 'PASSWORD',
version = '2017-05-26',
workspace_id = 'bec28d8f-18c1-4e97-8d08-9c842c658b51'
url = 'https://gateway-s.watsonplatform.net/conversation/api'

conversation = ConversationV1(
    username=username
    password=password,
    version=version,
    url=url
}

dialog_nodes = []

welcome_node = {
    'output': { 
        'text': { 'values': [ 'Welcome!' ],
                  'selection_policy': 'sequential' 
        } 
    },
    'parent': None,
    'context': None,
    'metadata': None,
    'next_step': None,
    'conditions': 'welcome',
    'dialog_node': 'Welcome',
    'previous_sibling': None
}

dialog_nodes.append(welcome_node)

# this appends to the previous node above, set by previous_sibling
node = {
    'dialog_node': 'greeting',
    'conditions': '#hello',
    'context': None,
    'metadata': None,
    'next_step': None,
    'output':{ 
        'text': { 'values': [ 'Hi! How can I help you?' ]},
                  'selection_policy': 'sequential'
        }
    },
    'title': 'greeting ',
    'previous_sibling': 'Welcome',
    'parent': None
}

dialog_nodes.append(node)

## Update the workspace.
response = conversation.update_workspace(
    workspace_id=workspace_id, 
    dialog_nodes=dialog_nodes
)

print(response)

此调用是全有或全无,因此如果您有现有节点,它将删除它们。原因是 SDK 没有单独的节点编辑。但这是一种更快的方法,而不是编辑单个节点(如果您有多个节点)。

如果你想进行单独调用,那么你将需要使用类似requests的东西,直到SDK更新。

示例(使用上面的相同变量):

import requests
from requests.auth import HTTPBasicAuth

endpoint = '{}/v1/workspaces/{}/dialog_nodes?version={}'.format(url,workspace_id,version)

basic_auth = HTTPBasicAuth(username, password)

# Change the condition to always execute. 
node['conditions'] = 'true'

response = requests.post(url=endpoint, auth=basic_auth, json=node)

就像 Simon O'Doherty 说的,你可以使用 Python SDK for using Conversation service. It is really the best practice to use the service, using the SDK or http requests

"If something's worth doing, it's worth doing right, right?". 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?".

You can use cURL command, yes, you can. But it hardly looks very Pythonic. That's a lot of work just for one little request. Python's is more than it.

词组 here.

的作者

但是,你的问题看起来你真的想在你的 Python 代码中使用 cURL 命令,所以,这里有一个例子。在这种情况下,您可以使用 subprocess.

呼叫对话:

import subprocess
subprocess.call(['curl', '-x', 'POST', '-H', '"Accept: application/json"', '-u', '{"userNameFromServiceCredentials:PasswordFromServiceCredentials" }', '"https://gateway-s.watsonplatform.net/conversation/api/v1/workspaces/bec28d8f-18c1-4e97-8d08-9c842c658b51/dialog_nodes?version=2017-05-26"'])

重要提示:要发送消息和获取输出,您需要使用函数subprocess.check_output(); like this . And send the message for the right router, your cURL command needs to looks like this example from and @Pridkkett.

注意:这个回答只是告诉你什么是更好的方法,如果你真的想use,一个"Stone path"给你关注。

  • API 将 Watson Conversation Service 与 Python.
  • 结合使用的参考
  • 请求文档 here

在python3用pycurl发送文件的时候想明白为什么要直接发送二进制文件 而不是提供它的路径

IBM 沃森

#coding:utf-8

import certifi
import pycurl
import io

response = io.BytesIO()
c = pycurl.Curl()

#"apikey" is a key word

user = "apikey"
pwd = "YouRaPiKey"

#the file you want to submit
with open("path_to_file/audio-file.flac","rb") as f:
    audiofile = f.read()

c.setopt(c.URL,  "https://api. .... /.... /.. /v1/recognize")
c.setopt(pycurl.USERPWD, "{}:{}".format(user, pwd))
c.setopt(c.WRITEFUNCTION, response.write)
c.setopt(c.HTTPHEADER, ['Content-Type: audio/flac','Transfer-Encoding: chunked'])
c.setopt(c.POSTFIELDS, audiofile)
c.setopt(c.CAINFO,certifi.where())


c.perform()
c.close()

body = response.getvalue()
body = body.decode('iso-8859-1')

print(body)