SunQuest
           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:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old August 7th, 2003, 02:05 AM
theperfectsoup theperfectsoup is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 35 theperfectsoup User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
a few socket questions...

Two Python-newbie questions...

Question #1:
Say I have a server-side socket that is set to listen, and I want it non-blocking. But when I try to do this:

Code:
>>> import socket
>>> server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> server_socket.bind((socket.gethostname(), 4000))
>>> server_socket.setblocking(0)
>>> server_socket.listen(5)
>>> (conn, addr) = server_socket.accept()


I get this...

Code:
  File "<pyshell#6>", line 1, in -toplevel-
    (conn, addr) = server_socket.accept()
  File "C:\dev\Python\lib\socket.py", line 167, in accept
    sock, addr = self._sock.accept()
error: (10035, 'The socket operation could not complete without blocking')


I realize I could do this as a quick fix:

Code:
insock, outsock, errsock = select.select([server_socket], [], [], 0)
# insock has at most 1 element, 0 if no connections pending...
for sock in insock:
	new_conn, addr = sock.accept()
	# do whatever with new_conn....


But that's hardly elegant, really. Is there a better way?

Question #2:
All my established connections are in an array of sockets (in fact, after I get new_conn above from server_socket.accept(), I append it to my array of sockets/established connections, and then perform select on that array -- which makes sense, because I want to find what sockets out of many need attention...). But after, say, 3 minutes of not sending/receiving data over a socket, i'd just like to send a packet and have it echo back to make sure the connection is still alive (I could define/code this function, but there wouldn't happen to be a built-in Python function that confirms the validity of a connection, would there?). After 5 minutes, I'd like to tear down the connection/close the socket. Can I bind timers (do they exist?) to socket objects to signal when 3/5 minutes have passed?

The other way (and probably easier way) I was thinking of would be when I send/receive over a socket, update a table that records when I last used the socket. Also, periodically go to the table and find sockets that haven't been used in over 3/5 minutes, and either check their validity/remove them. If the first way is possible, would this be better?

Thanks,
theperfectsoup

Last edited by theperfectsoup : August 7th, 2003 at 02:11 AM.

Reply With Quote
  #2  
Old August 7th, 2003, 05:18 AM
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
The problem is that, when you do setblocking(False), it really means settimeout(0.0). Then you do an accept, which means "wait for an incoming connection". Waiting for an incoming connection and having 0.0 as timeout value is sort of contradictory. (Or, at least, I think this is the problem.)

Maybe you should look at asyncore?

Last edited by percivall : August 7th, 2003 at 05:21 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > a few socket questions...


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 6 hosted by Hostway