Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > inetd script in python?

Reply
Thread Tools

inetd script in python?

 
 
Jan
Guest
Posts: n/a
 
      02-27-2004
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
 
Reply With Quote
 
 
 
 
Nick Craig-Wood
Guest
Posts: n/a
 
      02-27-2004
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

 
Reply With Quote
 
 
 
 
William Park
Guest
Posts: n/a
 
      02-28-2004
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( 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.
 
Reply With Quote
 
Donn Cave
Guest
Posts: n/a
 
      03-01-2004
In article <>,
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,
 
Reply With Quote
 
Donn Cave
Guest
Posts: n/a
 
      03-01-2004
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,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
client server socket interaction (inetd) Tim Python 7 02-18-2011 10:28 PM
DRb and xinetd / inetd Jon Evans Ruby 0 07-20-2006 05:08 PM
inetd like server socket supervisor in Java Sakagami Hiroki Java 2 06-07-2006 04:23 PM
Stdout to UDP socket in inetd Obi-Wan C Programming 3 03-11-2006 01:37 AM
SimpleXMLServer meets inetd Marek Kubica Python 0 03-12-2005 07:06 PM



Advertisments