socket.error: (9, 'Bad file descriptor')

Fabian Lienert fabian at mitlinks.ch
Sun Dec 29 10:03:42 EST 2002


Dear python group members,

While I am learning python I face the following problem:

I experienced with socket module. what i want is that a socket listens on a port
and prints something on request.

so i can start my socket that listens to port 99999. i can connect with "nc localhost 
99999", write "cpuload" and press enter. my little program gives me the right string back.
but the python interpreter says:
--
Connected by ('127.0.0.1', 33063)
Traceback (most recent call last):
   File "<stdin>", line 9, in ?
socket.error: (9, 'Bad file descriptor')
--

this comes from emacs python C-c C-c executing.
from shell i get:
--
# python2.2 python/socket5.py
Connected by ('127.0.0.1', 33064)
Traceback (most recent call last):
   File "python/socket5.py", line 9, in ?
     data = conn.recv(1024)
socket.error: (9, 'Bad file descriptor')
--

my python script:
--
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 99999))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
     data = conn.recv(1024)
     if not data: break
     elif data == 'cpuload\n' or data == 'cpuload' :
         conn.send("Hier die CPU Aktivitaet:\n")
#    conn.send(data + "\n")
     conn.close()
--
thanks a lot for any help in advance!
fabian





More information about the Python-list mailing list