inetd script in python?

J

Jan

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
 
N

Nick Craig-Wood

Jan said:
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!
 
W

William Park

Jan said:
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.
 
D

Donn Cave

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, (e-mail address removed)
 
D

Donn Cave

....
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, (e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top