|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
timed listening sockets
does anyone know of any way I can have a socket only listen for a given amount of time and then stop (even if noone makes a connection during that time)? I'm a newbie and this seems to me to be something that should be easily accomplishable, but I can't find it anywhere.
Any help is appreciated.....Thanks |
|
#2
|
|||
|
|||
|
Quote:
There is the settimeout method of the socket object. It'll set a timeout in seconds or no timeout (if given None). It'll then throw a "timeout" exception. |
|
#3
|
||||
|
||||
|
Quote:
I though you cant set a timeout limit on low level sockets, dont know if it will work, but if anything you can try socketserver, or you can download a model that will work with low level socket programming i know there was one someware.. just cant remember were
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#4
|
|||
|
|||
|
Thanks for replies.......I tried getting the settimeouts thing, as well as setdefaulttimeout, working for a loooong time before posting this, but to no avail....I ended up getting this to work by using the asyncore and asynchat modules. I had to rewrite the loop method inside asyncore to only loop for x seconds.....this isn't the best fix in the world but it works.
|
|
#5
|
|||
|
|||
|
socket.settimeout seems to work fine...
Code:
>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.bind(('127.0.0.1', 80))
>>> s.settimeout(2)
>>> s.accept()
(two seconds later)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python23\lib\socket.py", line 167, in accept
sock, addr = self._sock.accept()
timeout: timed out
>>>
What problems are/were you having with it? |
|
#6
|
|||
|
|||
|
Actually, it works fine now....before I was getting errors about blocking or something....Thanks for your help
|
|
#7
|
|||
|
|||
|
Ok....I figured out what I was doing wrong before.....
If you set a timeout (as you did above) it works fine.....however, if you attempt to send or receive data over that connection, it errors saying something like "cannot perform this action on non-blocking sockets"......so my natural reaction was to stick a sock.setblocking(1) immediately after my sock.settimeout(10).......however, it would seem to me that this then cancels out the timeout and my socket never timed out. However, if you put the sock.setblocking(1) AFTER the accept() call and BEFORE the first send or receive, all works fine....... Thanks a lot for your help with this. Roscoe |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > timed listening sockets |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|