client server socket interaction (inetd)

bobicanprogram icanbob at gmail.com
Thu Feb 17 21:47:07 CET 2011


On Feb 17, 2:41 pm, Martin Gregorie <mar... at address-in-sig.invalid>
wrote:
> On Thu, 17 Feb 2011 08:14:36 -0800, Tim wrote:
> > Hi, I have an inetd service on freebsd that calls a program (daemon.py)
> > with which I want the remote user to communicate.  I can call daemon.py
> > from the command line on the host machine and it works fine.
>
> > What I don't understand is how to make my remote client script actually
> > communicate. If I'm understanding correctly, the code below just takes a
> > message and sends it to inetd and writes the stdout from the process to
> > the client.
>
> > How can I modify the code to send a response back?
>
> The code you've shown would appear to be doing what you've specified,
> though only you can know whether this is what you intended.
> Each time you run the client it:
> - connects to the server
> - sends a request
> - reads the response(s)
> - closes the socket and exits.
>
> If you run it a second time it should do the same again. Is this the case?
>
> An inetd server should be started when a connection request is received.
> It should read requests, sending a response to each request in turn,
> until the connection is closed, when it will be stopped by inetd.
>
> Without seeing the code for the server and the corresponding inetd
> configuration line its not possible to say more.
>
> BTW, I prefer xinetd to inetd - its configuration is much more modular
> and easier to understand. If freebsd supports xinetd it may make life
> easier if you use it rather than inetd.
>
>    Here's the outline
>
>
>
> > of what I want to do:
> > (1) client sends the message to the server (client -> inetd ->
> > daemon.py),
> > (2) client receives output back from the server, (3) client user
> > responds to a question from the remote process (4) client continues to
> > receive output back.
>
> > where 2-3-4 happen as needed by the remote process. Cries out for a
> > while loop doesn't it? I just don't know what to put in it. Currently I
> > just have steps 1 and 2 working. Client sends one message and gets all
> > output back. If server asks a question, processes deadlock with server
> > waiting and client unable to respond. thanks,
> > --Tim Arnold
>
> > # imports, constants set
> > #def line_buffer(sock):
> > #    code to yield the string response in #    blocks of 1024 bytes
>
> > def client(ip,port,message):
> >     sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
> >     sock.connect((ip,port))
> >     sock.send(message)
> >     for line in line_buffer(sock):
> >         print line
> >     sock.close()
>
> > if __name__ == '__main__':
> >     message = ' '.join(sys.argv[1:])                                )
> >     print 'working... %s %s' % (SERVER_IP,SERVER_PORT)
> >     client(SERVER_IP,SERVER_PORT,message) print 'done.'
>
> --
> martin@   | Martin Gregorie
> gregorie. | Essex, UK
> org       |


Have a look at the SIMPL toolkit (http://www.icanprogram.com/06py/
lesson1/lesson1.html).  It will allow you to do exactly what you want
without having to dive into socket programming.

bob



More information about the Python-list mailing list