Another Socket in Python

Steven sadams123 at optushome.com.au
Sat Aug 17 19:41:22 CEST 2002


"Juza" <r0bbie at libero.it> wrote in message
news:Wjt79.73136$lu5.2099472 at twister1.libero.it...
> It's the same old problem in my code:
> import time
> import socket
> HOST = ''
> PORT = 50007
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.bind((HOST, PORT))
> s.listen(1)
> conn, addr = s.accept()
> print 'Connected by', addr
> while 1:
>     data = conn.recv(1024)
>     if not data: break
>     conn.send("Tutto OK")
> conn.close()
> time.sleep(10)
>
> i need a script that every second after the server is in LISTEM print on
the
> screen * until a client is logged in.
> I have try with the module select but i'm not able to do it.
> Any hint?

as others have said, could you post the code you have tried with 'select'?

IIRC one thing to remember with select is that  you don't pass the actual
socket in a list, you pass the file descriptor/number. You can get this by
using the socket objects fileno() method.

so you might have something like

------------------------
import time
import socket
import select
HOST = ''
PORT = 50007
TIMEOUT=1
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
connected=0
while not connected:
        in_socks,_out_socks,
exception_socks=select.select([s.fileno()],[],[], TIMEOUT)
        if s.fileno() in in_socks:
                conn, addr=s.accept()
                connected=1
        print "*"
# connected to conn, do whatever
----------------------------


Steven





More information about the Python-list mailing list