Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The answer by Larry Hastings has some great general advice about sockets, but there are a couple of mistakes as it pertains to how the <code>recv(bufsize)</code> method works in the Python socket module.</p> <p>So, to clarify, since this may be confusing to others looking to this for help:</p> <ol> <li>The bufsize param for the <code>recv(bufsize)</code> method is not optional. You'll get an error if you call <code>recv()</code> (without the param).</li> <li>The bufferlen in <code>recv(bufsize)</code> is a <em>maximum</em> size. The recv will happily return fewer bytes if there are fewer available.</li> </ol> <p>See <a href="http://docs.python.org/library/socket.html" rel="noreferrer">the documentation</a> for details.</p> <p>Now, if you're receiving data from a client and want to know when you've received all of the data, you're probably going to have to add it to your protocol -- as Larry suggests. See <a href="http://code.activestate.com/recipes/408859/" rel="noreferrer">this recipe</a> for strategies for determining end of message.</p> <p>As that recipe points out, for some protocols, the client will simply disconnect when it's done sending data. In those cases, your <code>while True</code> loop should work fine. If the client does <em>not</em> disconnect, you'll need to figure out some way to signal your content length, delimit your messages, or implement a timeout.</p> <p>I'd be happy to try to help further if you could post your exact client code and a description of your test protocol.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload