askance
askance

Reputation: 1157

HTTP Post request with Python JSON

I am trying to send data to a URL. I've a code to send it for each cpu on my Mac. But the current code loops through each of the cpustats and sends them 1 after the other. I need to send all of them in 1 POST 'cycle' but it should be formatted such that it sends it like this -

cpuStats = {nice: 123.0, idle:123.0....}
cpuStats = {nice: 123.0, idle:123.0....}
cpuStats = {nice: 123.0, idle:123.0....}

and so on...

Further, the current code pulls the stats from my Mac (with a '200 OK' for each cpustat) but when I run it on Linux, Windows, it just returns the prompt without giving any errors or stats. My guess is that it has to do with the 'break' at 'socket.error:' (My Mac has 4 cpus but the Linux and Windows machines on which I test it have 1 each.

import psutil 
import socket
import time
import sample
import json
import httplib
import urllib

serverHost = sample.host
port = sample.port

thisClient = socket.gethostname()
currentTime = int(time.time())
s = socket.socket()
s.connect((serverHost,port))
cpuStats = psutil.cpu_times_percent(percpu=True)


def loop_thru_cpus():
    global cpuStats
    for stat in cpuStats:
        stat = json.dumps(stat._asdict())

        try:

            command = 'put cpu.usr ' + str(currentTime) + " " + str(cpuStats[0]) + "host ="+ thisClient+ "/n"
            s.sendall(command)
            command = 'put cpu.nice ' + str(currentTime) + " " + str(cpuStats[1]) + "host ="+ thisClient+ "/n"
            s.sendall(command)
            command = 'put cpu.sys ' + str(currentTime) + " " + str(cpuStats[2]) + "host ="+ thisClient+ "/n"
            s.sendall(command)
            command = 'put cpu.idle ' + str(currentTime) + " " + str(cpuStats[3]) + "host ="+ thisClient+ "/n"
            s.sendall(command)

            params = urllib.urlencode({'cpuStats': stat, 'thisClient': 1234})
            headers = headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
            conn = httplib.HTTPConnection(serverHost, port)
            conn.request("POST", "", params, headers)
            response = conn.getresponse()
            print response.status, response.reason

        except IndexError:
            continue
        except socket.error:
            print "Connection refused"
            continue

    print stat

loop_thru_cpus()
s.close()

Upvotes: 0

Views: 672

Answers (1)

Slater Victoroff
Slater Victoroff

Reputation: 21924

If you're just trying to send the data all at once you should realize that you're not actually sending a dictionary across, but instead you're sending a string. In that case, you could easily send all the data in one go by just constructing your data like so:

data = "\n".join([json.dumps(stat._asdict()) for stat in cpuStats])

If that endpoint is someone else's that might not be sensible, but assuming that's your own endpoint you're pointing at it should be pretty trivial to unbundle this data.

Additionally I would HIGHLY suggest switching to the requests module over urllib as it extends all of the same functionality in a MUCH easier wrapper. For instance, in requests you would send that request by doing the following:

import requests

response = requests.post("your://url.here", data=data)
print response.content

Upvotes: 2

Related Questions