martes, 10 de enero de 2017

2017/01/10. BLOQUE 1. Sistemas Web (arquitectura REST y APIs web)



import httplib
import urllib
import psutil

CHANNEL_ID = ''
WRITE_API_KEY = ''

server = 'api.thingspeak.com'
conn = httplib.HTTPSConnection(server)
conn.connect()

while(True):
    cpuPercent = psutil.cpu_percent(interval=15)
    ramPercent = psutil.virtual_memory().percent
    print " CPU = " + str(cpuPercent) + "   RAM = " + str(ramPercent)

    uri = '/update.json'
    parametroak = {'api_key': WRITE_API_KEY,
                   'field1': str(cpuPercent),
                   'field2': str(ramPercent)}
    params_encoded = urllib.urlencode(parametroak)
    headers = {'Host': server,
               'Content-Type': 'application/x-www-form-urlencoded',
               'Content-Length': str(len(params_encoded))}

    conn.request('POST', uri, headers=headers, body=params_encoded)
    erantzuna = conn.getresponse()
    print "STATUS: " + str(erantzuna.status)
    print "CONTENT: " + erantzuna.read()

No hay comentarios:

Publicar un comentario