forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudiot_http_example.py
More file actions
161 lines (135 loc) · 5.6 KB
/
Copy pathcloudiot_http_example.py
File metadata and controls
161 lines (135 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Python sample for connecting to Google Cloud IoT Core via HTTP, using JWT.
This example connects to Google Cloud IoT Core via HTTP, using a JWT for device
authentication. After connecting, by default the device publishes 100 messages
to the server at a rate of one per second, and then exits.
Before you run the sample, you must register your device as described in the
README in the parent folder.
"""
import argparse
import base64
import datetime
import json
import time
import jwt
import requests
_BASE_URL = 'https://cloudiot-device.googleapis.com/v1beta1'
def create_jwt(project_id, private_key_file, algorithm):
"""Creates a JWT (https://jwt.io) to authenticate this device.
Args:
project_id: The cloud project ID this device belongs to
private_key_file: A path to a file containing either an RSA256 or
ES256 private key.
algorithm: The encryption algorithm to use. Either 'RS256' or
'ES256'
Returns:
A JWT generated from the given project_id and private key, which
expires in 20 minutes. After 20 minutes, your client will be
disconnected, and a new JWT will have to be generated.
Raises:
ValueError: If the private_key_file does not contain a known key.
"""
token = {
# The time the token was issued.
'iat': datetime.datetime.utcnow(),
# Token expiration time.
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=60),
# The audience field should always be set to the GCP project id.
'aud': project_id
}
# Read the private key file.
with open(private_key_file, 'r') as f:
private_key = f.read()
print('Creating JWT using {} from private key file {}'.format(
algorithm, private_key_file))
return jwt.encode(token, private_key, algorithm=algorithm)
def parse_command_line_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description=(
'Example Google Cloud IoT Core HTTP device connection code.'))
parser.add_argument(
'--project_id', required=True, help='GCP cloud project name')
parser.add_argument(
'--registry_id', required=True, help='Cloud IoT Core registry id')
parser.add_argument(
'--device_id', required=True, help='Cloud IoT Core device id')
parser.add_argument(
'--private_key_file',
required=True,
help='Path to private key file.')
parser.add_argument(
'--algorithm',
choices=('RS256', 'ES256'),
required=True,
help='The encryption algorithm to use to generate the JWT.')
parser.add_argument(
'--cloud_region', default='us-central1', help='GCP cloud region')
parser.add_argument(
'--ca_certs',
default='roots.pem',
help=('CA root from https://pki.google.com/roots.pem'))
parser.add_argument(
'--num_messages',
type=int,
default=100,
help='Number of messages to publish.')
parser.add_argument(
'--message_type',
choices=('event', 'state'),
default='event',
required=True,
help=('Indicates whether the message to be published is a '
'telemetry event or a device state message.'))
parser.add_argument(
'--base_url',
default=_BASE_URL,
help=('Base URL for the Cloud IoT Core Device Service API'))
return parser.parse_args()
def main():
args = parse_command_line_args()
# Publish to the events or state topic based on the flag.
url_suffix = 'publishEvent' if args.message_type == 'event' else 'setState'
publish_url = (
'{}/projects/{}/locations/{}/registries/{}/devices/{}:{}').format(
args.base_url, args.project_id, args.cloud_region,
args.registry_id, args.device_id, url_suffix)
jwt_token = create_jwt(
args.project_id, args.private_key_file, args.algorithm)
headers = {
'Authorization': 'Bearer {}'.format(jwt_token),
'Content-Type': 'application/json'
}
# Publish num_messages mesages to the HTTP bridge once per second.
for i in range(1, args.num_messages + 1):
payload = '{}/{}-payload-{}'.format(
args.registry_id, args.device_id, i)
print('Publishing message {}/{}: \'{}\''.format(
i, args.num_messages, payload))
body = None
if args.message_type == 'event':
body = {'binary_data': base64.urlsafe_b64encode(payload)}
else:
body = {
'state': {'binary_data': base64.urlsafe_b64encode(payload)}
}
resp = requests.post(
publish_url, data=json.dumps(body), headers=headers)
print('HTTP response: ', resp)
# Send events every second. State should not be updated as often
time.sleep(1 if args.message_type == 'event' else 5)
print('Finished.')
if __name__ == '__main__':
main()