title: Python JSON POST request
body: <p>Different questions re: 1 script. I have to JSON-ify from a Python script that'll run on client-side and do a POST on a remote URL. I am following the docs here <a href="http://docs.python.org/2/library/httplib.html" rel="nofollow">http://docs.python.org/2/library/httplib.html</a> - However, I am not sure if I am doing it right. I am not getting any response-status either when I run it on my Mac. Right now, I have doubts about the following - </p>
  
  <pre><code>(1) The dummy 'device_key' (of the client) 
  (2) The IP, Port listed at HTTPConnection --- I don't want to hard-code the IP, Port - should I be using "ServerHost:Port"
  (3) The cpuStats is a nametuple (of 'user'= somevalue, 'idle' = somevalue, etc.) that is converted to a dict by _asdict(). I want to just send the cpuStats (namedtuple) to the URLpage.
  (4) The cpu_times_percent(percpu=True) is what the docs say &lt;http://code.google.com/p/psutil/wiki/Documentation#CPU&gt; but when I run the script on my Mac - it shows just 1 namedtuple of cpu percentages though my mac has 4 cpus. 
  </code></pre>
  
  <p>I have a strong feeling that my errors extend beyond this list.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>import psutil 
  import socket
  import time
  import sample
  import json
  import httplib
  import urllib
  
  
  
  serverHost = sample.host
  port = sample.port
  
  thisClient = socket.gethostname()
  cpuStats = psutil.cpu_times_percent(percpu=True)
  print cpuStats
  
  
  currentTime = int(time.time())
  s = socket.socket()
  s.connect((serverHost,port))
  
  
  
  cpuStatsjson = json.dumps(cpuStats._asdict())
  params = urllib.urlencode({'cpuStats': cpuStats, 'device_key': 12345})
  headers = {"Content-type": "application/json", "Accept": "text/plain"}
  conn = httplib.HTTPConnection("http://XXX.XXX.XXX.XXX:YYYY")
  conn.request("POST", "", cpuStatsjson, headers)
  response = conn.getresponse()
  print response.status, response.reason
  
  s.close()
  </code></pre>