2015-02-01 2 views
0

셸 또는 파이썬 스크립트에 대한 많은 경험이 없으므로이 작업을 수행하는 방법에 대한 도움이 필요합니다.cURL 셸 스크립트 파일에 인수 전달

목표 : 컬 포스트 요청 또는 파이썬 POST 요청 중 하나를 수행하는 데 사용됩니다 쉘 또는 파이썬 스크립트 파일

패스 인수.

이의 내가 파이썬 경로를 이동하고 파일 이름은 내가 몸과 대상 값에 인수를 전달하는 방법과 그 명령 줄을 통해 볼 것 가겠어요 어떻게 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 

라고하자?

감사

+0

음과 같이 것이다 CLI에

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

명령 줄 인수를 구문 분석 argparse를 사용해보십시오 : 방법 파이썬 스크립트에 커맨드 라인 인수를 부여 하시겠습니까? –

답변

1

이 다시 초까지

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

좋습니다. 나는 그것을 내일 사격 할 것이고 만약 내가 다른 질문이 있으면 나는 여기에 또 다른 의견을 게시 할 것이다. 도와 주셔서 감사합니다. – jimrice

+0

위대한 작품. 컬 스크립트에 어떻게 접근 할 것입니까? curl script.sh와 비슷하게 보일 것이라고 가정합니다. bash와 논쟁은 있지만 실제 스크립트에서 어떻게 보이겠습니까? 위의 인수 파서 예제와 다르다고 가정합니다. – jimrice

+0

나는 그 질문을 이해할 것이라고 생각하지 않는다. 파이썬 스크립트에서 컬의 모든 기능을 어떻게 복제 할 것인가? – Greg

0

사용 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 
관련 문제