콘텐츠로 건너뛰기
Home » instapush로 메시지 푸시 전송 요청을 보내보자 » Page 2

instapush로 메시지 푸시 전송 요청을 보내보자

curl을 사용하는 방법

curl이 설치된 시스템이라면, 파이썬에서 subprocess 모듈을 사용해서 간단하게 외부 프로세스로 curl을 기동하여 요청을 전송할 수 있다. API 페이지 상단의 예제의 포맷을 그대로 사용하면 된다.

from subprocess import check_call
import json
## check_call 은 명령 실행 후 출력된 내용을 문자열로 리턴해준다. 
def send_push_curl(name, message):
  payload = { 'event' : 'Test_Event',
              'trackers': { 'name' : name, 'message':message }}
  data = json.dumps(payload, ensure_ascii=False) ## ensure_ascii=False는 비라틴문자를 위한 옵션
  cmds = ['curl',
      '-X', 'POST',
      '-H', 'x-instapush-appid : 3###########',
      '-H', 'x-instapush-appsecret : 5################',
      '-H', 'Content-Type: application/json',
      '-d', data,  ## 인코딩하지 않은 문자열을 사용해야한다.
      'https://api.instapush.im/v1/post']
  result = check_call(cmds, shell=True, universal_newline=True)
  print(result)  

 

instapush 패키지를 사용하는 방법

어떻게 보면 가장 쉬운 방법이다. 앞에서 소개한 직접 HTTP 요청을 만들어서 보내는 과정을 간단히 래핑한 패키지가 이미 제공된다. pip install instapush 로 설치해서 다음과 같이 사용할 수 있다.

from instapush import App
app = App(appid='3##########', secret='5##############')
app.notify(event_name='Test_Event', trackers={'name' : 'sooop', 'message': 'hello, world'})

 

Pages: 1 2