472,783 Members | 1,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 software developers and data experts.

Uncatchable socket.error in socket.py (?)

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
Jul 18 '05 #1
6 5416
Rune wrote:
Now, enough words: What I wonder is: Can I catch that socket.error?


By not doing

from socket import *

but rather

import socket

(and prefixing your code with "socket." where it's needed).

The socket.error you tried to catch is coming from the socket
object that you placed in your namespace by doing the "from"
import. And the socket class doesn't have an "error" attribute ;)

--Irmen

Jul 18 '05 #2

Irmen de Jong wrote:
Rune wrote:
Now, enough words: What I wonder is: Can I catch that socket.error?


By not doing

from socket import *

but rather

import socket

(and prefixing your code with "socket." where it's needed).

The socket.error you tried to catch is coming from the socket
object that you placed in your namespace by doing the "from"
import. And the socket class doesn't have an "error" attribute ;)


Thanks Irmen, but the problem seems to be that the server is actually
crashed before I can catch the exception. There is no connection to
send output to. I have no idea how to avoid this.

Rune

Jul 18 '05 #3
Rune wrote:
Thanks Irmen, but the problem seems to be that the server is actually
crashed before I can catch the exception. There is no connection to
send output to. I have no idea how to avoid this.


If you catch the error, then the server won't have "crashed," and you'll
be able to recover however you like.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ The most exhausting thing in life is being insincere.
\__/ Anne Morrow Lindbergh
Jul 18 '05 #4
Erik Max Francis wrote:
Rune wrote:
Thanks Irmen, but the problem seems to be that the server is actually
crashed before I can catch the exception. There is no connection to
send output to. I have no idea how to avoid this.


If you catch the error, then the server won't have "crashed," and you'll
be able to recover however you like.


I am able to catch the exception, but thie connection object from
connection, address = sockobj.accept(), is lost.

I cannot connect to the server any longer and connection.send(data)
will generate a new socket.error: (9, 'Bad file descriptor')

I can't find a way out of this but to restart the server.

R

Jul 18 '05 #5
Rune wrote:
I cannot connect to the server any longer and connection.send(data)
will generate a new socket.error: (9, 'Bad file descriptor')


I see; it wasn't entirely clear to me what your complaint was from your
first message. If you're getting an exception when calling recv, the
connection has already dropped. You can't send the message because the
connection isn't there anymore. Catching or not catching an exception
won't change that.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Peace with a cudgel in hand is war.
\__/ (a Portugese proverb)
Jul 18 '05 #6
> > I cannot connect to the server any longer and connection.send(data)
will generate a new socket.error: (9, 'Bad file descriptor')


I see; it wasn't entirely clear to me what your complaint was from your
first message. If you're getting an exception when calling recv, the
connection has already dropped. You can't send the message because the
connection isn't there anymore. Catching or not catching an exception
won't change that.


Guido handles this situation using timemachine exceptions:

try:
communicationcode()
unrecoverable_except socket.error:
alternatecode()

This works as expected. If the communciation code raises an
unrecoverable exception, then time is backed up to just before
the communication code was executed and the alternate code
is run instead.

I believe he intends to incorporate this feature in his next
language, Voyager, which is named after a Star Trek spin-off.
The idea is not really new; it is just an extension of commit/rollback
for socketlike objects where the state (uncrashed) is not easily
restored without access to timemachine hardware.
Raymond Hettinger
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Johannes Eble | last post by:
Hello Python community, I am trying the echo-client and echo-server examples in Chapter 10, "Programming Python" by Mark Lutz. It is probably the most simple sockets sample: A socket server just...
5
by: kingofearth.com | last post by:
When I try to open a socket with python i get the following error: Traceback (most recent call last): File "./mailer", line 3, in ? sock = socket.socket(socket.PF_INET, socket.SOCK_STREAM)...
3
by: Magnus Lycka | last post by:
I have a socket server like below which I want to exit when it's out of data. If I interrupt the client, I'll get a broken pipe on the server side, and after a Ctrl-C, I can restart the server...
7
by: | last post by:
Hi all, I have a simple .aspx page running on net 2.0 that is trying to do a http post to a remote server. Here is the code Private Function ProcessRequests(ByVal strbody As String) As String...
3
by: JuHui | last post by:
I wrote a script to get 100 pages from a server. like below: 1:import httplib 2:conns = httplib.HTTPConnection("www.mytest.com") 3:conn.request("GET", "/") sometimes a socket error was...
2
by: tcarasu | last post by:
We have two production servers under the DNS setup. We have installed our web services on both the servers. The web application is also installed in the same servers. When the web application tries...
0
by: palmem | last post by:
I am trying to write a simple FTP server in order to learn about sockets This is my first time trying sockets This code should take a connection on port 8110, dump it to a client "thread" (not...
2
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept...
0
by: Jesper | last post by:
We have a .NET 2.0 desktop application which sends and receives network packets over UDP. Several users have reported an occasional socket error 10052 which happens when the code calls...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.