<div dir="ltr">dataReceived gets called with any data that is available on the socket. That might not be all data you sent on the other side. To ensure complete "messages" are delivered your application has to specify some framing, such as Netstrings. <div>
<br></div><div>See: <a href="http://twistedmatrix.com/documents/current/api/twisted.protocols.basic.NetstringReceiver.html">http://twistedmatrix.com/documents/current/api/twisted.protocols.basic.NetstringReceiver.html</a> and the original specification of netstrings <a href="http://cr.yp.to/proto/netstrings.txt">http://cr.yp.to/proto/netstrings.txt</a><div>
<br></div><div style>That being said, it's a very bad idea to send pickles over the network because unpickling can result in arbitrary code execution.</div><div style><br></div><div style>Peruse some of the results of <a href="https://www.google.com/search?q=pickle+execute+arbitrary+code">https://www.google.com/search?q=pickle+execute+arbitrary+code</a> for examples of these dangers.</div>
<div style><br></div><div style>-David</div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 28, 2013 at 6:24 PM, <a href="mailto:succer110@tiscali.it">succer110@tiscali.it</a> <span dir="ltr"><<a href="mailto:succer110@tiscali.it" target="_blank">succer110@tiscali.it</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>I have made 2 application: </div><div>The client extract data from a sql server (10k lines), and send every line pickled to a "collector" server via socket.</div>
<div>The server uses twisted and receive every line, unpikle it and store the data in another sql server.</div><div><br></div><div>Everytime i start sending data from client to server, in the first 200 line (everytime a different line) **the server** throws an exception:</div>
<div>SOMETIMES it something like:</div><div><br></div><div> Traceback (most recent call last):</div><div> File "collector2.py", line 81, in dataReceived</div><div> self.count,account<span style="white-space:pre-wrap">        </span>= pickle.loads(data)</div>
<div> File "/usr/lib/python2.6/pickle.py", line 1374, in loads</div><div> return Unpickler(file).load()</div><div> File "/usr/lib/python2.6/pickle.py", line 858, in load</div><div> dispatch[key](self)</div>
<div> File "/usr/lib/python2.6/pickle.py", line 1138, in load_pop</div><div> del self.stack[-1]</div><div> IndexError: list assignment index out of range</div><div><br></div><div>But it's NOT every time the same. Printing my exception i red: </div>
<div>Exception: pop from empty list</div><div>Exception: list index out of range</div><div>Exception: "'"</div><div>Exception: list assignment index out of range</div><div><br></div><div>Another strange errors is:</div>
<div>File "/usr/lib/python2.6/pickle.py", line 1124, in find_class</div><div> __import__(module)</div><div>exceptions.ImportError: No module named ond'</div><div><br></div><div> for i in listaSAI:</div>
<div> <span style="white-space:pre-wrap">        </span>crm={}</div><div> try:</div><div> <span style="white-space:pre-wrap">                </span>crm['uid']=i[0]</div><div> <span style="white-space:pre-wrap">        </span>except:</div>
<div> <span style="white-space:pre-wrap">                </span>crm['uid']=None</div><div> <span style="white-space:pre-wrap">        </span>try:</div><div> <span style="white-space:pre-wrap">                </span>crm['type_cond']=i[01]</div>
<div> <span style="white-space:pre-wrap">        </span>except:</div><div> <span style="white-space:pre-wrap">                </span>crm['type_cond']=None</div><div> <span style="white-space:pre-wrap">        </span>try:</div><div> <span style="white-space:pre-wrap">                </span>crm['population_id']=i[2]</div>
<div> <span style="white-space:pre-wrap">        </span>except:</div><div> <span style="white-space:pre-wrap">                </span>crm['population_id']=None</div><div> <span style="white-space:pre-wrap">        </span>try:</div><div>
<span style="white-space:pre-wrap">                </span>crm['product_id']=i[3]</div><div> <span style="white-space:pre-wrap">        </span>except:</div><div> <span style="white-space:pre-wrap">                </span>crm['product_id']=None</div>
<div> <span style="white-space:pre-wrap">        </span>try:</div><div> <span style="white-space:pre-wrap">                </span>crm['a_id']=i[4]</div><div> <span style="white-space:pre-wrap">        </span>except:</div><div> <span style="white-space:pre-wrap">                </span>crm['a_id']=None</div>
<div> <span style="white-space:pre-wrap">        </span>try:</div><div> <span style="white-space:pre-wrap">                </span>crm['status']=i[5]</div><div> <span style="white-space:pre-wrap">        </span>except:</div><div> <span style="white-space:pre-wrap">                </span>crm['status']=None</div>
<div> <span style="white-space:pre-wrap">        </span>#time.sleep(0.001)</div><div> <span style="white-space:pre-wrap">        </span>serialized = pickle.dumps((count,crm))</div><div> <span style="white-space:pre-wrap">        </span>#print "sent num", count, crm</div>
<div> <span style="white-space:pre-wrap">        </span>s.sendall(serialized)</div><div> <span style="white-space:pre-wrap">        </span>count += 1</div><div><br></div><div><br></div><div>And my server:</div><div><br></div><div>
def dataReceived(self, data):</div><div> <span style="white-space:pre-wrap">        </span>try:</div><div> <span style="white-space:pre-wrap">                </span>self.count,account<span style="white-space:pre-wrap">        </span>= pickle.loads(data) </div>
<div> <span style="white-space:pre-wrap">        </span>except Exception as e:</div><div> <span style="white-space:pre-wrap">                </span>print "Eccezione:", e</div><div> <span style="white-space:pre-wrap">                </span>print self.count+1</div>
<div> <span style="white-space:pre-wrap">                </span>print data</div><div> <span style="white-space:pre-wrap">                </span>print traceback.print_exc()</div><div><br></div><div><br></div><div><br></div><div>Printing the data in my client tells me that everything it's ok.</div>
<div>*If i try to slow down the process of sending using time.sleep(0.01) in my client, EVERYTHING IS FINE, and no exception are raised.*</div><div><br></div><div>What can i do to debug my code?</div><div><br></div><div>p.s. </div>
<div>I suspect that exceptions.ImportError: No module named ond' refers to "type_cond" key in crm.</div><br><br>Invita i tuoi amici e Tiscali ti premia! Il consiglio di un amico vale più di uno spot in TV. Per ogni nuovo abbonato 30 € di premio per te e per lui! Un amico al mese e parli e navighi sempre gratis: <a href="http://freelosophy.tiscali.it/" target="_blank">http://freelosophy.tiscali.it/</a> <br>
<br><br>_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
<br></blockquote></div><br></div>