-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp_https_data_interface_read_write_example.py
More file actions
231 lines (183 loc) · 7.28 KB
/
http_https_data_interface_read_write_example.py
File metadata and controls
231 lines (183 loc) · 7.28 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#==============================================================================
# http_https_data_interface_read_write.py
# Python script that tests https and http socket level calls
# for basic data interface API to Exosite One Platform
#
#==============================================================================
## Tested with python 2.6.5
##
## Copyright (c) 2012, Exosite LLC
## All rights reserved.
##
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are met:
##
## * Redistributions of source code must retain the above copyright notice,
## this list of conditions and the following disclaimer.
## * Redistributions in binary form must reproduce the above copyright
## notice, this list of conditions and the following disclaimer in the
## documentation and/or other materials provided with the distribution.
## * Neither the name of Exosite LLC nor the names of its contributors may
## be used to endorse or promote products derived from this software
## without specific prior written permission.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
## POSSIBILITY OF SUCH DAMAGE.
"""
Exosite HTTP and HTTPS POST/GET REST API example (HELLO WORLD!) using http
level calls
"""
"""
Directions:
1) Have an Exosite Portals account
2) Add a new Device client to your portal. Click on it to get it's CIK (KEY)
3) Add the CIK below where it says 'PUTYOURCIKHERE'. (inside of the quotes)
4) On your portal, add two data sources. This script does not auto-create the data sources.
Data Source 1 should be called 'message', type is string, resource is 'message'
Data Source 2 should be called 'number', type is integer, resource is 'number'
5) Run this python script in Python 2.6.5 or higher.
6) Assuming your computer has an active network connection, you should see data sent to
these data sources on your portal. The script runs only once. It should also print out
what it sent, since it is doing a 'read' after the 'write'.
"""
import urllib
import httplib
cik = 'PUTYOURCIKHERE'
server = 'm2.exosite.com'
SHOW_DEBUG = 0 #Set to 1 for verbose debug output of connection or set to 0 to disable
print '========================================================================'
print 'HTTP REST API DEMO. Using CIK ', cik
if SHOW_DEBUG == 1:
print 'DEBUG CONNECTION OUTPUT ENABLED'
print '========================================================================'
print '\r\n'
http_port = 80
url = '/api:v1/stack/alias'
params = urllib.urlencode({'message': 'hello world!', 'number': 1 }) #Dataport have alias 'message' and 'number'
headers = {'X-Exosite-CIK': cik, 'content-type': 'application/x-www-form-urlencoded; charset=utf-8'}
print '=================='
print 'POST'
print '=================='
print 'Server:', server
print 'Port:', http_port
print 'URL: ', url
print 'Data: ', params
print ' '
conn = httplib.HTTPConnection(server,http_port)
if SHOW_DEBUG == 1:
conn.set_debuglevel(1)
print 'DEBUG OUTPUT:'
print '------------------------'
print '--DEBUG MESSAGE OUTPUT--'
conn.request("POST",url,params,headers)
if SHOW_DEBUG == 1:
print '--DEBUG RESPONSE OUTPUT--'
response = conn.getresponse();
if SHOW_DEBUG == 1:
print '------------------------'
print 'response: ',response.status,response.reason
data = response.read() #Expected Resposne is 204 No Content (since you are writing data, nothing to return)
end = data.find('<')
if -1 == end: end = len(data)
print '(Note: You should see a response of "204 No Content" if this works correctly)'
conn.close()
print '\r\n\r\n'
url = '/api:v1/stack/alias?message&number'
headers = {'Accept':'application/x-www-form-urlencoded; charset=utf-8','X-Exosite-CIK':cik}
print '=================='
print 'GET'
print '=================='
print 'Server:', server
print 'Port:', http_port
print 'URL: ', url
print ' '
conn = httplib.HTTPConnection(server,http_port)
if SHOW_DEBUG == 1:
conn.set_debuglevel(1)
print 'DEBUG OUTPUT:'
print '------------------------'
print '--DEBUG MESSAGE OUTPUT--'
conn.request("GET",url,"",headers)
if SHOW_DEBUG == 1:
print '--DEBUG RESPONSE OUTPUT--'
response = conn.getresponse();
if SHOW_DEBUG == 1:
print '------------------------'
print 'response: ',response.status,response.reason
data = response.read()
print 'response data:', urllib.unquote(data)
conn.close()
print '\r\n'
print '\r\n'
print '========================================================================'
print 'HTTPS REST API DEMO. Using CIK ', cik
if SHOW_DEBUG == 1:
print 'DEBUG CONNECTION OUTPUT ENABLED'
print '========================================================================'
print '\r\n'
https_port = 443
url = '/api:v1/stack/alias'
params = urllib.urlencode({'message': 'hello world!', 'number': 1 }) #Dataport have alias 'message' and 'number'
headers = {'X-Exosite-CIK': cik, 'content-type': 'application/x-www-form-urlencoded; charset=utf-8'}
print '=================='
print 'POST'
print '=================='
print 'Server:', server
print 'Port:', https_port
print 'URL: ', url
print 'Data: ', params
print ' '
conn = httplib.HTTPSConnection(server,https_port)
if SHOW_DEBUG == 1:
conn.set_debuglevel(1)
print 'DEBUG OUTPUT:'
print '------------------------'
print '--DEBUG MESSAGE OUTPUT--'
conn.request("POST",url,params,headers)
if SHOW_DEBUG == 1:
print '--DEBUG RESPONSE OUTPUT--'
response = conn.getresponse();
if SHOW_DEBUG == 1:
print '------------------------'
print 'response: ',response.status,response.reason
data = response.read() #Expected Resposne is 204 No Content (since you are writing data, nothing to return)
end = data.find('<')
if -1 == end: end = len(data)
print '(Note: You should see a response of "204 No Content" if this works correctly)'
conn.close()
print '\r\n\r\n'
url = '/api:v1/stack/alias?message&number'
headers = {'Accept':'application/x-www-form-urlencoded; charset=utf-8','X-Exosite-CIK':cik}
print '=================='
print 'GET'
print '=================='
print 'Server:', server
print 'Port:', https_port
print 'URL: ', url
print ' '
conn = httplib.HTTPSConnection(server,https_port)
if SHOW_DEBUG == 1:
conn.set_debuglevel(1)
print 'DEBUG OUTPUT:'
print '------------------------'
print '--DEBUG MESSAGE OUTPUT--'
conn.request("GET",url,"",headers)
if SHOW_DEBUG == 1:
print '--DEBUG RESPONSE OUTPUT--'
response = conn.getresponse();
if SHOW_DEBUG == 1:
print '------------------------'
print 'response: ',response.status,response.reason
data = response.read()
print 'response data:', urllib.unquote(data)
conn.close()