【问题标题】:passing arguments to a cURL shell script file将参数传递给 cURL shell 脚本文件
【发布时间】:2015-02-01 10:49:06
【问题描述】:

我对 shell 或 python 脚本没有太多经验,所以我正在寻找一些关于如何实现这一点的帮助。

目标:

将参数传递给将用于执行 cURL Post 请求或 python post 请求的 shell 或 python 脚本文件。

假设我走python路线,文件名是api.py

import json,httplib
connection = httplib.HTTPSConnection('api.example.com', 443)
connection.connect()
connection.request('POST', '/message', json.dumps({
       "where": {
         "devicePlatform": "andriod"
       },
       "data": {
         "body": "Test message!",
         "subject": "Test subject"
       }
     }), {
       "X-Application-Id": "XXXXXXXXX",
       "X-API-Key": "XXXXXXXX",
       "Content-Type": "application/json"
     })
result = json.loads(connection.getresponse().read())
print result

我将如何为正文和主题值传递参数,以及通过命令行看起来如何?

谢谢

【问题讨论】:

  • 好吧,请稍等一下:如何获取 Python 脚本的命令行参数?

标签: python shell curl command-line-arguments


【解决方案1】:

尝试使用 argparse 解析命令行参数

from argparse import ArgumentParser
import json

import httplib

parser = ArgumentParser()
parser.add_argument("-s", "--subject", help="Subject data", required=True)
parser.add_argument("-b", "--body", help="Body data", required=True)
args = parser.parse_args()
connection = httplib.HTTPSConnection('api.example.com', 443)
connection.connect()
connection.request('POST', '/message', json.dumps({
       "where": {
         "devicePlatform": "andriod"
       },
       "data": {
         "body": args.body,
         "subject": args.subject,
       }
...

在 CLI 上看起来像

python script.py -b "Body" -s "Subject"

【讨论】:

  • 好的,这是有道理的。明天我会试一试,如果我有任何其他问题,我会在这里发表另一条评论。感谢您的帮助。
  • 这很好用。您将如何处理 curl 脚本。我假设它看起来类似于 curl script.sh | bash 和这里的参数,但是这些在实际脚本中的外观如何?我假设它们与上面的参数解析器示例不同。
  • 我不认为我理解这个问题。您是在问如何在 python 脚本中复制 curl 的所有功能?
  • 这里是一个例子。此请求的大部分将具有常量值,例如用户和密码内容类型等。此 curl 请求可能位于 .sh 文件 bash 脚本中。主题、描述和电子邮件值需要是变量。有没有办法完成我们对 bash 脚本中的参数所做的事情。如果不是,我怎么能在下面(来自一个常见的 api)这个例子并在 python 中完成。身份验证是基本访问身份验证,并且在作为授权标头 (-u user@yourcompany.com:test) 传递之前必须进行 base64 编码。下面的例子
  • curl -u user@yourcompany.com:test -H "Content-Type: application/json" -d '{ "helpdesk_ticket": { "description": "有关问题的详细信息... ","主题":"需要支持...","电子邮件":"tom@outerspace.com","优先级":1,"状态":2 },"cc_emails":"ram@freshdesk.com, diana@freshdesk.com" }' -X POST domain.freshdesk.com/helpdesk/tickets.json
【解决方案2】:

使用argparse。一个主题的例子:

import json,httplib
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('subject', help='string containing the subject')
args = parser.parse_args()

connection = httplib.HTTPSConnection('api.example.com', 443)
connection.connect()
connection.request('POST', '/message', json.dumps({
       "where": {
         "devicePlatform": "andriod"
       },
       "data": {
         "body": "Test message!",
         "subject": args.subject
       }
     }), {
       "X-Application-Id": "XXXXXXXXX",
       "X-API-Key": "XXXXXXXX",
       "Content-Type": "application/json"
     })
result = json.loads(connection.getresponse().read())
print result

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2022-01-23
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多