The solution would be<br><br>
timeout = s.gettimeout()<br>
s.settimeout(3)<br><div><div class="im"><span> b</span><span>=</span><span>time</span><span>.</span><span>clock</span><span>()</span><span><br></span><span> while</span><span> time</span><span>.</span><span>clock</span><span>()-</span><span>b</span><span><</span><span>3</span><span> </span><span>:</span></div>
<span>
try :<br> data=s.recv(1024)<br> except :<br> break<br></span></div> s.settimeout(timeout)<br><br>Am I right ?<br><br>Dwayne<br><br><div class="gmail_quote">2011/2/4 Dwayne Blind <span dir="ltr"><<a href="mailto:dwayneblind@gmail.com">dwayneblind@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Thanks Stephen. It's really nice of you.<br><br>I have not understood everything though. (I have never used a context manager before.)<br>
<br>Here are some comments : <br><br> timeout = s.gettimeout() # Is that the default timeout ?<br>
s.settimeout(3) # I guess this is a 3 second timeout<br>
s.recv(1024)<br>
s.settimeout(timeout) # You change it back ?<br><br>So with a while loop, it should be :<div class="im"><br><br>
timeout = s.gettimeout()<br>
s.settimeout(3)<br></div><div><div class="im"><span> b</span><span>=</span><span>time</span><span>.</span><span>clock</span><span>()</span><span><br></span><span> while</span><span> time</span><span>.</span><span>clock</span><span>()-</span><span>b</span><span><</span><span>3</span><span> </span><span>:</span></div>
<span> <br>
data=s.recv(1024)</span></div> s.settimeout(timeout)<br>
<br>
Am I right ?<br><br>Thanks again,<br>Dwayne<br><br><br><div class="gmail_quote"><div class="im">2011/2/3 Stephen Hansen <span dir="ltr"><me+list/<a href="mailto:python@ixokai.io" target="_blank">python@ixokai.io</a>></span><br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div>On 2/3/11 10:13 AM, Dwayne Blind wrote:<div><div></div><div class="h5"><br>
> Thanks for your answer. I don't want to reset my socket. I want to apply<br>
> the timeout to the rcv method only.<br>
<br>
</div></div></div><div><div></div><div class="h5">Setting the timeout does not "reset [your] socket", I don't think. And I<br>
get that you want to only timeout recv... that's why I pointed out its a<br>
socket method, not an argument to recv. If you don't want it to apply to<br>
everything else, you just have to be sure to change it back after recv.<br>
<br>
Just:<br>
timeout = s.gettimeout()<br>
s.settimeout(3)<br>
s.recv(1024)<br>
s.settimeout(timeout)<br>
<br>
Personally, I'd prefer to do:<br>
<br>
with timeout(s, 3):<br>
s.recv(1024)<br>
<br>
That's a lot more clear, and I'd roll this context manager to accomplish it:<br>
<br>
--- start<br>
<br>
from contextlib import contextmanager<br>
<br>
@contextmanager<br>
def timeout(sock, timeout):<br>
old_timeout = sock.gettimeout()<br>
sock.settimeout(timeout)<br>
try:<br>
yield sock<br>
finally:<br>
sock.settimeout(old_timeout)<br>
<br>
--- end<br>
<br>
The contextmanager decorator is an easy/quick way of making a context<br>
manager. Everything up until the yield is executed before the 'with'<br>
block is run, and everything after the yield is executed after the<br>
'with' block concludes.<br>
<br>
If the with block throws an exception, it'll be catchable at the yield<br>
point.<br>
<font color="#888888"><br>
--<br>
</font><div><div></div><div><br>
Stephen Hansen<br>
... Also: Ixokai<br>
... Mail: me+list/python (AT) ixokai (DOT) io<br>
... Blog: <a href="http://meh.ixokai.io/" target="_blank">http://meh.ixokai.io/</a><br>
<br>
</div></div></div></div></blockquote></div><br>
</blockquote></div><br>