Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 18th, 2004, 03:21 PM
roscoe04 roscoe04 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 10 roscoe04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old May 18th, 2004, 04:10 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Quote:
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.

Reply With Quote
  #3  
Old May 18th, 2004, 05:03 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,553 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 110977 Folding Title: Super Ultimate Folder - Level 1Folding Points: 110977 Folding Title: Super Ultimate Folder - Level 1Folding Points: 110977 Folding Title: Super Ultimate Folder - Level 1Folding Points: 110977 Folding Title: Super Ultimate Folder - Level 1Folding Points: 110977 Folding Title: Super Ultimate Folder - Level 1Folding Points: 110977 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 3 h 36 m 8 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
Quote:
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

Reply With Quote
  #4  
Old May 18th, 2004, 05:27 PM
roscoe04 roscoe04 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 10 roscoe04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #5  
Old May 18th, 2004, 07:32 PM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 10
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?

Reply With Quote
  #6  
Old May 20th, 2004, 05:59 PM
roscoe04 roscoe04 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 10 roscoe04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Actually, it works fine now....before I was getting errors about blocking or something....Thanks for your help

Reply With Quote
  #7  
Old May 21st, 2004, 10:52 AM
roscoe04 roscoe04 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 10 roscoe04 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > timed listening sockets


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT