How to send a json object using tcp socket in python
<p>here is my python tcp client. I want to send a json object to the server.But I can't send the object using the sendall() method. how can I do this?</p>

<pre><code>import socket
import sys
import json

HOST, PORT = "localhost", 9999

m ='{"id": 2, "name": "abc"}'
jsonObj = json.loads(m)


data = jsonObj

# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # Connect to server and send data
    sock.connect((HOST, PORT))
    sock.sendall(jsonObj)


    # Receive data from the server and shut down
    received = sock.recv(1024)
finally:
    sock.close()

print "Sent:     {}".format(data)
print "Received: {}".format(received)
</code></pre>

<p>Skip the <code>json.loads()</code> part. Send the json object as the json string and load it from the string at the TCP client.</p>

<p>Also check: <a href="http://stackoverflow.com/questions/6341823/python-sending-dictionary-throught-tcp">Python sending dictionary throught TCP</a></p>

python json sockets tcp