0

I do not have much experience with shell or python scripts so I am looking for some help on how I can accomplish this.

Goal:

Pass arguments to a shell or python script file that will be used to perform either a cURL Post request or a python post request.

Let's say I go the python route and the filename is 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

How would I go about passing in arguments to for the body and subject values and how would that look via command line?

Thanks

1
  • Well, back up for a second: how would you grab command line arguments to a Python script? Commented Feb 1, 2015 at 2:45

2 Answers 2

1

Try using argparse to parse command line arguments

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,
       }
...

On the CLI it would look like

python script.py -b "Body" -s "Subject"
Sign up to request clarification or add additional context in comments.

6 Comments

Ok this makes sense. I will give it a shot tomorrow and if I have any other questions I will post another comment here. Thanks for the help.
This worked great. How would you approach for a curl script. I am assuming it would look similar to curl script.sh | bash and the arguments here but how would those look in the actual script? I am assuming they are different than the argument parser example above.
I don't think I understand the question. Are you asking how would you replicate all the functionality of curl in a python script?
Here is an example. Most of this request would have constant values like the user and password content type etc. This curl request could be in a .sh file bash script. The subject, description and email values would need to be variables. Is there a way to accomplish what we did with the arguments in a bash script. If not how could I take this example below (from a common api) and accomplish in python. Authentication is Basic Access Authentication and must be base64 encoded prior to be passed as an authorization header (-u [email protected]:test) . Example below
curl -u [email protected]:test -H "Content-Type: application/json" -d '{ "helpdesk_ticket": { "description": "Details about the issue...", "subject": "Support Needed...", "email": "[email protected]", "priority": 1, "status": 2 }, "cc_emails": "[email protected],[email protected]" }' -X POST domain.freshdesk.com/helpdesk/tickets.json
|
0

Use argparse. An example with the subject:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.