Skip to content

Instantly share code, notes, and snippets.

@bzshang
Created October 14, 2015 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bzshang/c4fc105c329095e0ca05 to your computer and use it in GitHub Desktop.
Save bzshang/c4fc105c329095e0ca05 to your computer and use it in GitHub Desktop.
Send random normal number at even intervals to PI Web API
import urllib2, urllib, random, time, hmac, hashlib, base64
from datetime import datetime
def piweb():
print 'starting'
value_endpoint = '<your PI Web API value endpoint>'
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, value_endpoint, '<your username>', '<your password>')
# decorator pattern
auth = urllib2.HTTPBasicAuthHandler(password_manager) # create an authentication handler
opener = urllib2.build_opener(auth) # create an opener with the authentication handler
urllib2.install_opener(opener) # install the opener...
headers = {'Content-Type': 'application/json', 'Accept': '*/*'}
for i in range(0,10):
ts = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f') + 'Z'
print ts
body = str({"Timestamp": ts, "Value": random.normalvariate(0,1)})
request = urllib2.Request(value_endpoint, headers = headers)
request.add_data(body)
response = urllib2.urlopen(request).read()
time.sleep(1)
if __name__ == '__main__':
piweb()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment