low level networking in python
Maxim Veksler
hq4ever at gmail.com
Fri Apr 6 10:32:50 EDT 2007
On 4/4/07, Irmen de Jong <irmen.NOSPAM at xs4all.nl> wrote:
> Maxim Veksler wrote:
>
> > I'm trying to bind a non-blocking socket, here is my code:
> > """
> > #!/usr/bin/env python
> >
> > import socket, select
> > from time import sleep
> >
> > s_nb10000 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> > s_nb10000.setblocking(0)
> >
> > s_nb10000.bind(('192.168.2.106', 10002))
> > s_nb10000.listen(5)
> >
> > while 1:
> > conn, addr = s_nb10000.accept()
> > ready_to_read, ready_to_write, in_error = select.select([conn], [],
> > [], 0)
> > print (ready_to_read, ready_to_write, in_error)
> > sleep(100)
> >
> > s_nb10000.close()
> > """
> >
> > And this is the exception I'm getting:
> > """
> > python non_blocking_socket.py
> > Traceback (most recent call last):
> > File "non_blocking_socket.py", line 13, in ?
> > conn, addr = s_nb10000.accept()
> > File "/usr/lib/python2.4/socket.py", line 161, in accept
> > sock, addr = self._sock.accept()
> > socket.error: (11, 'Resource temporarily unavailable')
> > """
> >
> > What am I doing wrong here?
>
> Nothing.
> Any operation on a non-blocking socket that is usually blocking
> (this includes accept(), bind(), connect(), recv with MSG_WAITALL)
> can possibly return a socket.error with errno set to EAGAIN.
> ('resource temporarily unavailable').
> If this happens you should use a select() on the socket to
> wait until it's done with the requested operation.
>
Hello everyone, I would like to thank you all for the helping tips so
far, with your help I managed to improve the previous code to not give
the error, I believe it's now working.
The non blocking echo socket code:
"""
#!/usr/bin/env python
import socket, select
s_nb10000 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s_nb10000.setblocking(0)
s_nb10000.bind(('0.0.0.0', 10002))
s_nb10000.listen(5)
while 1:
ready_to_read, ready_to_write, in_error =
select.select([s_nb10000], [], [], 0)
if s_nb10000 in ready_to_read:
conn, addr = s_nb10000.accept()
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
s_nb10000.close()
"""
> --Irmen
>
Maxim.
--
Cheers,
Maxim Veksler
"Free as in Freedom" - Do u GNU ?
More information about the Python-list
mailing list