inetd script in python?

Discussion in 'Python' started by Jan, Feb 27, 2004.

  1. Jan

    Jan Guest

    Hi all!

    I looked around a lot, but I didn't find a solution. I have to write
    an simple server application to do some jobs and return a result to a
    client. I tried SocketServer and this works really fine. But I didn't
    manage to get a script working that is started by inetd.

    So far I read about that I could use stdin and stdout for
    communication since inetd handles the socket. And I read that I could
    use socket.fromfd to get a socket object to work with. But both do not
    work, I tried to send just a greeting back to the client or getting
    some text - nothing.

    Is there any working example out there - I coudn't find one. There was
    just a snippet like this:

    import socket
    import sys
    client = socket.fromfd(sys.stdin.fileno(), socket.AF_INET,
    socket.SOCK_STREAM )
    while 1:
    bfr = client.recv(1024)
    client.send(bfr)

    And a similar one with 0 instead of sys.stdin.fileno(). Only thing I
    get is this error:

    socket.error: (134, 'Transport endpoint is not connected')

    (Same thing when using stdin/stdout - I'm trying to connect via a
    telnet client on a Solaris8/sparc machine).

    Any hints?
    Jan
    Jan, Feb 27, 2004
    #1
    1. Advertising

  2. Jan <> wrote:
    > So far I read about that I could use stdin and stdout for
    > communication since inetd handles the socket. And I read that I
    > could use socket.fromfd to get a socket object to work with. But
    > both do not work, I tried to send just a greeting back to the
    > client or getting some text - nothing.


    You should find something simple like this works - inetd is supposed
    to make writing internet daemons easy!

    import sys

    while 1:
    bfr = sys.stdin.read(1024)
    sys.stdout.write(bfr)

    You can then test the code on the command line by typing stuff to it
    and seeing it print its output.

    Beware buffering though!

    --
    Nick Craig-Wood
    Nick Craig-Wood, Feb 27, 2004
    #2
    1. Advertising

  3. Jan

    William Park Guest

    Jan <> wrote:
    > Hi all!
    >
    > I looked around a lot, but I didn't find a solution. I have to write
    > an simple server application to do some jobs and return a result to a
    > client. I tried SocketServer and this works really fine. But I didn't
    > manage to get a script working that is started by inetd.
    >
    > So far I read about that I could use stdin and stdout for
    > communication since inetd handles the socket. And I read that I could
    > use socket.fromfd to get a socket object to work with. But both do not
    > work, I tried to send just a greeting back to the client or getting
    > some text - nothing.
    >
    > Is there any working example out there - I coudn't find one. There was
    > just a snippet like this:
    >
    > import socket
    > import sys
    > client = socket.fromfd(sys.stdin.fileno(), socket.AF_INET,
    > socket.SOCK_STREAM )


    No. inetd(8) will invoke your script, with stdin/stdout/stderr attached
    to incoming connection. It will take care of all socketing. And, all
    you have to do is read from stdin and write to stdout/stderr.

    You can skim over
    http://home.eol.ca/~parkw/index.html#httpd
    for an example of inetd daemon script. It's shell script, but Python
    script would go similar way.

    --
    William Park, Open Geometry Consulting, <>
    Linux solution for data management and processing.
    William Park, Feb 28, 2004
    #3
  4. Jan

    Donn Cave Guest

    In article <-wood.com>,
    Nick Craig-Wood <> wrote:
    > Jan <> wrote:
    > > So far I read about that I could use stdin and stdout for
    > > communication since inetd handles the socket. And I read that I
    > > could use socket.fromfd to get a socket object to work with. But
    > > both do not work, I tried to send just a greeting back to the
    > > client or getting some text - nothing.

    >
    > You should find something simple like this works - inetd is supposed
    > to make writing internet daemons easy!
    >
    > import sys
    >
    > while 1:
    > bfr = sys.stdin.read(1024)
    > sys.stdout.write(bfr)
    >
    > You can then test the code on the command line by typing stuff to it
    > and seeing it print its output.
    >
    > Beware buffering though!


    Indeed, to the point where the code you propose would not
    work when you test it the way you propose.

    Donn Cave,
    Donn Cave, Mar 1, 2004
    #4
  5. Jan

    Donn Cave Guest

    In article <>,
    (Jan) wrote:
    ....

    > import socket
    > import sys
    > client = socket.fromfd(sys.stdin.fileno(), socket.AF_INET,
    > socket.SOCK_STREAM )
    > while 1:
    > bfr = client.recv(1024)
    > client.send(bfr)
    >
    > And a similar one with 0 instead of sys.stdin.fileno(). Only thing I
    > get is this error:
    >
    > socket.error: (134, 'Transport endpoint is not connected')
    >
    > (Same thing when using stdin/stdout - I'm trying to connect via a
    > telnet client on a Solaris8/sparc machine).


    That's odd - works for me on MacOS X. This is a Berkeley
    UNIX platform, though, and it sounds like you may have a
    system there where sockets are a layer on an AT&T STREAMS
    network implementation - the `transport endpoint' thing
    is my clue on that. That's one thing to look at - maybe
    in that system, socket operations really aren't supported
    by whatever TLI module they use with inetd. Try posix.write
    and posix.read instead. They may work, while Berkeley socket
    functions don't.

    On the other hand, inetd is really supposed to give you a
    unit 0 & 1 that support socket operations, and a platform
    that fails to do that would be pretty unpopular, I would
    think. I guess it would have the same effect if socket.fromfd
    is not working right on your platform, so that might be
    something to look into if you are in a position to do that.

    Donn Cave,
    Donn Cave, Mar 1, 2004
    #5
    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. Sakagami Hiroki

    inetd like server socket supervisor in Java

    Sakagami Hiroki, Jun 7, 2006, in forum: Java
    Replies:
    2
    Views:
    2,089
    Martin Gregorie
    Jun 7, 2006
  2. Marek Kubica

    SimpleXMLServer meets inetd

    Marek Kubica, Mar 12, 2005, in forum: Python
    Replies:
    0
    Views:
    314
    Marek Kubica
    Mar 12, 2005
  3. Obi-Wan

    Stdout to UDP socket in inetd

    Obi-Wan, Mar 10, 2006, in forum: C Programming
    Replies:
    3
    Views:
    442
    Keith Thompson
    Mar 11, 2006
  4. Tim
    Replies:
    7
    Views:
    587
    Nobody
    Feb 18, 2011
  5. Jon  Evans

    DRb and xinetd / inetd

    Jon Evans, Jul 20, 2006, in forum: Ruby
    Replies:
    0
    Views:
    92
    Jon Evans
    Jul 20, 2006
Loading...

Share This Page