Python server from inetd?
Tim Roberts
timr at probo.com
Wed Apr 10 23:44:46 EDT 2002
Erno Kuusela <erno-news at erno.iki.fi> wrote:
>Tim Roberts <timr at probo.com> writes:
>
>| #! /usr/bin/python
>| import socket
>
>| client = socket.fromfd( 0, socket.AF_INET, socet.SOCK_STREAM )
>| while 1:
>| bfr = client.read(1024)
>| if not bfr:
>| break
>| ...
>
>| But this does not work. The "read" call returns 0 immediately, over and
>| over, until the client exits. I never get any data.
>
>it shouldn't work, since socket objects don't have a read method, only
>recv. getting 0 sounds really odd, since both s.recv nor f.read only
>return strings. are you sure you're interpreting this correctly?
>can you post the real code?
You're right. That's what I get for posting from memory instead of from a
file. I did figure out that I needed to use stdin and skip the
translate-to-socket model.
Here's the code in its most recent incarnation. This prints "Got empty
buffer" forever.
import os
import sys
import time
log = open('/tmp/tinetd.log','a')
sys.stderr = log
sys.stderr.write( 'Hello\n' )
# Suck all data from the socket until a line ends with Ctrl-D.
bfr = ''
while 1:
bfr1 = sys.stdin.readline()
if bfr1:
log.write( "Got %d\n", len(bfr1) )
bfr = bfr + bfr1
if bfr[-1] == chr(4):
log.write( "Got ctrl-d\n" )
break
else:
log.write( "Got empty buffer\n" )
log.write( repr(bfr) )
log.write( '\n' )
--
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list