#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    10
    Rep Power
    0

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    11
    Originally Posted by roscoe04
    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)?
    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.
  4. #3
  5. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by percivall
    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.
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    10
    Rep Power
    0
    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    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?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    10
    Rep Power
    0
    Actually, it works fine now....before I was getting errors about blocking or something....Thanks for your help
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    10
    Rep Power
    0
    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

IMN logo majestic logo threadwatch logo seochat tools logo