Uncatchable socket.error in socket.py (?)

Rune nospam at the.net
Tue Aug 26 13:47:43 EDT 2003


Hi,
I've written a very simple 'kill-server' to help me shut down
processes through Telnet or HTTP. The kill-server is a function and is
launched  as a thread. I use the module socket.py on Python v2.3
(Windows)

I can use telnet host:port and enter the secret killword or
use a broser with http://host:port/secret_killword

The 'kill-server' validates the secret_killword and writes a file
to /tmp. The main process regularly checks for the existence of that
file, and ends the loop if it does exist.

It works well.

But most for the fun (because this will probably never be 
exposed to the net, i think), I started to implement
som basic security. 
For instance: I don't want ther server to be easily crashable.

So I've written some try/except blocks to handle irregularities,
but when trying to overflow the receive buffer, I get an exception
in the socket module itself

It says:
|---------------------->
Unhandled exception in thread started by <function spawn_killserver at
0x00B5EEB0>
Traceback (most recent call last):
  File "X:\tmp\webcam.py", line 68, in spawn_killserver
    connection.send("Nice try mister.")
  File "G:\dev\python\lib\socket.py", line 143, in _dummy
    raise error(EBADF, 'Bad file descriptor')
socket.error: (9, 'Bad file descriptor')
<----------------------|


Relevant code:
#init SOCKET
sock = socket(AF_INET, SOCK_STREAM)
sock.bind((host, killserver_port))
sock.listen(5)
(blah blah)

connection, address = sock.accept()

try:
	buffer = connection.recv(16)
except:
	connection.send("Nice try mister.")
	connection.close()

So if I use a browser with this in the address field: 
   
   http://host:port/500_chars_long_string

I get the execption 'socket.error:'

But if I try to catch it with 'except socket.error:',
I generate a new exception:

   AttributeError - _socketobject' has no attribute 'error'

I know I can make the receive buffer bigger, but the point 
is to lock that door once and for all. 

It seems to me that what happens is that the server crashes,
and that connection.send() no longer have any place to send to.

Now, enough words: What I wonder is: Can I catch that socket.error?

--rune




More information about the Python-list mailing list