thedark Second Lieutenant
Joined: 30 Jul 2005
Posts: 1074
|
Posted: Thu Aug 04, 2005 1:25 pm Post subject: AmigaPython Example: 'finger' for ARexx |
|
|
Enough talk. Let's see some Python to give you an idea. Note that this example is for AmigaPython only because it uses the ARexx extension!
A finger tool for ARexx - just like the 'finger' command you know to find out about a user. Is it possible? YES! First let's make a ARexx host in Python which provides us with a FINGER ARexx command. Here is the source:
# Python interface to the Internet finger daemon.
import sys, string
import socket
import ARexx
FINGER_PORT = 79
def finger(host, args):
print host,args
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(host, FINGER_PORT)
s.send(args + '\n')
reply=''
while 1:
---buf = s.recv(1024)
---if not buf: break
---reply=reply+buf
---return reply
# The function to handle the arexx commands,
def cmd_dispatcher(host,msg,cmd,args):
try:
---if cmd=='FINGER':
------msg.result=finger(args['HOST'],args['USER'])
except socket.error,string:
---msg.rc=ARexx.RC_ERROR
---msg.rc2=string[0]
---return 1
h=ARexx.host()
h.setcommand('FINGER','HOST/A,USER',{'USER':''},cmd_dispatcher)
print 'The ARexx port name is ',h.name
h.run()
(N.B. In Python, indentation matters. Thanks to the limits of HTML, the above code doesn't show the correct indentation. When copying, remember to add 3 blank spaces in place of --- )
We have an interface to the finger daemon in Python, and a full-fledged ARexx host which provides us with two commands: HELP and FINGER. The last line (h.run()) activates the message loop which will handle all commands until you press CTRL-C. HELP is a default command which is provided by the ARexx.host class for you, including a simple implementation. I defined the FINGER command with arguments:-
FINGER HOST/A,USER
The hostname to finger is necessary while the user name may be omitted (it defaults to the empty string). Now, let's see how we can access this ARexx host when it's running:
/* Finger client in ARexx */
options results
address 'PYTHON' /* fill in your port name here */
say 'FINGER @localhost...'
FINGER 'localhost'
say RESULT
say 'FINGER root@localhost...'
FINGER 'localhost root'
say RESULT
HELP FINGER
say 'The help string on the FINGER command is:' RESULT
HELP BOGUSCOMMAND
if RC=0 then do
say RESULT
end
else do
say 'An error occured for HELP BOGUSCOMMAND:' RC2
end
Voila! Of course you could now use AmigaPython's ARexx capabilities to write another Python script which duplicates this ARexx script's behavior and calls the ARexx host (AmigaPython running the Python script above in another shell), effectively using ARexx as an interprocess communication layer. I'll leave this as an exercise to the reader. |
|