gcloud 의 Pub/Sub 를 python3 로 실시한다

참고로 한 페이지
Publishing Messages
Subscribing to Messages

프로그램을 실행하려면,
인증 JSON 파일이 필요합니다.
이 예에서는 my-project-sep-10-2017.json이라고 했습니다.

이 JSON(서비스 계정 키)은 google console에서 만들고 다운로드합니다.


주제 만들기

create_topic.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   create_topic.py
#
#                       Feb/08/2018
# ------------------------------------------------------------------
import  sys 
from google.cloud import pubsub

# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
publisher = pubsub.PublisherClient()

project_id = 'my-project-sep-10-2017'
topic_name = 'topic_1'
topic_path = publisher.topic_path(project_id,topic_name)

try:
    topic = publisher.create_topic(topic_path)
    print('Topic created: {}'.format(topic))
except Exception as ee:
    sys.stderr.write("*** error *** in publisher.create_topic ***\n")
    sys.stderr.write(str(ee) + "\n")

sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

실행 스크립트
export GOOGLE_APPLICATION_CREDENTIALS="./my-project-sep-10-2017.json"
./create_topic.py

구독 만들기

create_subscribe.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   create_subscribe.py
#
#                       Feb/08/2018
# ------------------------------------------------------------------
import  sys 
from google.cloud import pubsub
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
subscriber = pubsub.SubscriberClient()

topic_name = 'projects/my-project-sep-10-2017/topics/topic_1'
sub_name = 'projects/my-project-sep-10-2017/subscriptions/subscription_1'

try:
    subscriber.create_subscription(sub_name, topic_name)
except Exception as ee:
    sys.stderr.write("*** error *** in subscriber.create_subscription ***\n")
    sys.stderr.write(str(ee) + "\n")

#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

실행 스크립트
export GOOGLE_APPLICATION_CREDENTIALS="./my-project-sep-10-2017.json"
./create_subscribe.py

게시

iot_publish.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   iot_publish.py
#
#                       Feb/08/2018
# ------------------------------------------------------------------
import  sys 
import  datetime
from google.cloud import pubsub
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
publisher = pubsub.PublisherClient()

project_id = 'my-project-sep-10-2017'
topic_name = 'topic_1'
topic_path = publisher.topic_path(project_id,topic_name)

dd = datetime.datetime.today()
message = 'This is my message. ' + dd.strftime("%Y-%m-%d %H:%M:%S") 
sys.stderr.write(message + "\n")

message_bb = message.encode()

publish_client = pubsub.PublisherClient()
publish_client.publish(topic_path, message_bb)

sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

실행 스크립트
export GOOGLE_APPLICATION_CREDENTIALS="./my-project-sep-10-2017.json"
./iot_publish.py

구독

iot_subscribe.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   iot_subscribe.py
#
#                       Feb/08/2018
# ------------------------------------------------------------------
import  sys 
from google.cloud import pubsub
# ------------------------------------------------------------------
def callback(message):
    print(message)  # Replace this with your actual logic.
    message.ack()
#
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
subscriber = pubsub.SubscriberClient()

sub_name = 'projects/my-project-sep-10-2017/subscriptions/subscription_1'

subscription = subscriber.subscribe(sub_name)

future = subscription.open(callback)

try:
    try:
        future.result()
    except Exception as ex:
        subscription.close()
        sys.stderr.write(str(ex) + "\n")
except KeyboardInterrupt:
    sys.stderr.write('*** interrupted! ***\n')
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

실행 스크립트
export GOOGLE_APPLICATION_CREDENTIALS="./my-project-sep-10-2017.json"
./iot_subscribe.py

좋은 웹페이지 즐겨찾기