Uncatchable socket.error in socket.py (?)

Discussion in 'Python' started by Rune, Aug 26, 2003.

  1. Rune

    Rune Guest

    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
     
    Rune, Aug 26, 2003
    #1
    1. Advertising

  2. 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
     
    Irmen de Jong, Aug 26, 2003
    #2
    1. Advertising

  3. Rune

    Rune Guest

    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
     
    Rune, Aug 26, 2003
    #3
  4. 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 && && 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
     
    Erik Max Francis, Aug 26, 2003
    #4
  5. Rune

    Rune Guest

    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
     
    Rune, Aug 26, 2003
    #5
  6. 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 && && 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)
     
    Erik Max Francis, Aug 26, 2003
    #6
  7. > > 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
     
    Raymond Hettinger, Aug 26, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. seth
    Replies:
    0
    Views:
    361
  2. Laszlo Nagy
    Replies:
    1
    Views:
    4,943
    Mark Wooding
    Jan 27, 2009
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    996
    Jean-Paul Calderone
    Jan 27, 2009
  4. Laszlo Nagy
    Replies:
    0
    Views:
    571
    Laszlo Nagy
    Feb 1, 2009
  5. Steve Holden
    Replies:
    0
    Views:
    689
    Steve Holden
    Feb 1, 2009
Loading...

Share This Page