Python JSON POST not working

This is the place for queries that don't fit in any of the other categories.

Python JSON POST not working

Postby askance » Fri Sep 20, 2013 10:27 pm

I am getting a '200 OK' for this script (to POST data) but for some reason the data is not being received at the server end. It could be an issue with the server/receiving code, but I have serious doubts about it since an earlier script with only a socket connection works fine and the server has no problem receiving the data. What I am trying to do is collect cpu stats and send it as JSON to the server. Since the stats are o/p as <ordereddict> for each cpu which, contains pairs for cpu.user, cpu.nice, cpu.idle, etc. I use the for-loop to create a dict with key and list of its corresponding values. The 'doseq' argument then binds each value to its key with an '&' separating each such pair. 'json.dumps' converts it into a string.

Code: Select all
import psutil
import time
import sample
import json
import httplib
import urllib

# Server and port address pulled in from
serverHost =
port = sample.port

# Populating current stats, time
currentTime = int(time.time())
cpuStats = psutil.cpu_times_percent(percpu=True)

# Looping through each cpu
stats = [stat._asdict() for stat in cpuStats]
print stats

# Building a list of values for each cpu-component as a unique key
merged = {}
for d in stats:
    for k, v in d.items ():
        if k not in merged: merged [k] = []
        merged [k].append (v)
# Building and binding pairs of (cpu-component and each of it's values) for all cpus
k = urllib.urlencode(merged, doseq=True)
print type(k)
print k

# Converting the stats into JSON
j = json.dumps(k)
print j

# Preparing the data, connecting and sending it to the server
params = urllib.urlencode({'CpuStats': merged, 'deviceKey': 1, 'timeStamp': str(currentTime)})
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
print params
print type(params)

Running it gives -------

Code: Select all
[OrderedDict([('user', 0.0), ('nice', 0.0), ('system', 0.0), ('idle', 100.0)]), OrderedDict([('user', 0.0), ('nice', 0.0), ('system', 100.0), ('idle', 0.0)]), OrderedDict([('user', 0.0), ('nice', 0.0), ('system', 0.0), ('idle', 100.0)]), OrderedDict([('user', 0.0), ('nice', 0.0), ('system', 0.0), ('idle', 100.0)])]
<type 'str'>
200 OK
<type 'str'>

My question is, given the task,what could I doing wrong here. The task again, is to send the cpustats to the server as JSON.
Follow up question: If the server is conf. to accept a string with an array of dicts, what do I change in the script.

The thorn is that cpustats has as many instances of cpu.user, cpu.nice, cpu.idle as there are cpus. So, I cannot make a simple dict out of it since user, nice, idle,... are keys and keys must be unique. So, to send the same key-value pair names multiple times, I am using the 'doseq' argument. 'doseq' cannot be applied to a string and therefore I am using 'doseq' pror to 'json.dumps'. So very confused :(
Last edited by micseydel on Fri Sep 20, 2013 10:43 pm, edited 1 time in total.
Reason: First post lock.
Posts: 5
Joined: Fri Sep 20, 2013 10:25 pm

Re: Python JSON POST not working

Postby stranac » Sat Sep 21, 2013 7:33 am

You should probably not be urlencoding if you are expected to post json.

Also, there are better ways to do http requests than httplib.
There's urllib in the standard library, but requests is way simpler to use.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 8 guests