For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2004 > Can someone translate a small .PY to Perl?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Can someone translate a small .PY to Perl?
GMane Python

2004-12-27, 8:55 pm

Hello Everyone.

Whil e reading the Python Cookbook as a means of learning Python, I
came across the script by Nicola Larosa. Not knowing anything about PERL, I
was wondering if there were a translation in PERL so I could have my Netware
servers send heartbeats to the heartbeat server?
I am beginning to learn the Python language after a 10-year
programming 'vacation' from my last class in college. Looking at this, I
think it'll end up being a rather quick translation, but although I'm
searching for translations, I'd really like to be able to just get my
Netware server to send heartbeats and not take flack from my boss for
'having to learn PERL' just to get the server to send a beat packet, so
that's why I'm asking for someone's help who knows the syntax of the
language.

Thanks!

Dave


Title: PyHeartbeat - detecting inactive computers
Submitter: Nicola Larosa


# Filename: HeartbeatClient.py

"""Heartbeat client, sends out an UDP packet periodically"""

import socket, time

SERVER_IP = '127.0.0.1'; SERVER_PORT = 43278; BEAT_PERIOD = 5

print ('Sending heartbeat to IP %s , port %d\n'
'press Ctrl-C to stop\n') % (SERVER_IP, SERVER_PORT)
while True:
hbSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
hbSocket.sendto('PyHB', (SERVER_IP, SERVER_PORT))
if __debug__: print 'Time: %s' % time.ctime()
time.sleep(BEAT_PERIOD)




Andrew Gaffney

2004-12-27, 8:55 pm

GMane Python wrote:
> Hello Everyone.
>
> Whil e reading the Python Cookbook as a means of learning Python, I
> came across the script by Nicola Larosa. Not knowing anything about PERL, I
> was wondering if there were a translation in PERL so I could have my Netware
> servers send heartbeats to the heartbeat server?
> I am beginning to learn the Python language after a 10-year
> programming 'vacation' from my last class in college. Looking at this, I
> think it'll end up being a rather quick translation, but although I'm
> searching for translations, I'd really like to be able to just get my
> Netware server to send heartbeats and not take flack from my boss for
> 'having to learn PERL' just to get the server to send a beat packet, so
> that's why I'm asking for someone's help who knows the syntax of the
> language.
>
> Thanks!
>
> Dave
>
>
> Title: PyHeartbeat - detecting inactive computers
> Submitter: Nicola Larosa
>
>
> # Filename: HeartbeatClient.py
>
> """Heartbeat client, sends out an UDP packet periodically"""
>
> import socket, time
>
> SERVER_IP = '127.0.0.1'; SERVER_PORT = 43278; BEAT_PERIOD = 5
>
> print ('Sending heartbeat to IP %s , port %d\n'
> 'press Ctrl-C to stop\n') % (SERVER_IP, SERVER_PORT)
> while True:
> hbSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> hbSocket.sendto('PyHB', (SERVER_IP, SERVER_PORT))
> if __debug__: print 'Time: %s' % time.ctime()
> time.sleep(BEAT_PERIOD)
>
>
>
>
>


Both Perl or Python are overkill for this. This can be done with a very simple
bash script. It only requires netcat.

#!/bin/bash

SERVER_IP="127.0.0.1"
SERVER_PORT=43278
BEAT_PERIOD=5

while `/bin/true`;
do
echo "pyHB" | nc -u -q 0 $SERVER_IP $SERVER_PORT
sleep ${BEAT_PERIOD}s
done

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.

Andrew Gaffney

2004-12-27, 8:55 pm

S. David Rose wrote:
> Sorry, sir. I did not state it boldly enough, but this is to be used on a
> Netware server. There is no port for Python on Netware, but PERL is
> available. Obviously, bash scripting will not work ....
>
> It's Netware 5.0 / 5.1 for my servers.


Ah, sorry. I didn't catch that part. Also, reply to the list instead of the sender.

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.

Randal L. Schwartz

2004-12-28, 3:55 am

>>>>> "Andrew" == Andrew Gaffney <agaffney@skylineaero.com> writes:

Andrew> while `/bin/true`;

Uh, what?

Execute /bin/true,
take its output
if its output is non-null, continue.

Last I checked, /bin/true outputs nothing. :)

Maybe you wanted:

while true;
do; ...; done

Or more commonly:

while :; do
...
done

because the ":" command is true.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Andrew Gaffney

2004-12-28, 3:55 am

Randal L. Schwartz wrote:
>
>
> Andrew> while `/bin/true`;
>
> Uh, what?
>
> Execute /bin/true,
> take its output
> if its output is non-null, continue.
>
> Last I checked, /bin/true outputs nothing. :)
>
> Maybe you wanted:
>
> while true;
> do; ...; done
>
> Or more commonly:
>
> while :; do
> ...
> done
>
> because the ":" command is true.


It works for me. That's the way I'd seen it done when I was learning bash. I
believe the while checks the return value, not the output of the command.

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.

Luke Bakken

2004-12-28, 3:56 pm

> > Or more commonly:
>=20
> It works for me. That's the way I'd seen it done when I was=20
> learning bash. I=20
> believe the while checks the return value, not the output of=20
> the command.


Just be thankful you didn't use 'cat' uselessly.

There is definitely no reason to use a separate command just for a true
value in a while loop.
Dave Gray

2004-12-28, 3:56 pm

> was wondering if there were a translation in PERL so I could have my Netware
> servers send heartbeats to the heartbeat server?
>
> Title: PyHeartbeat - detecting inactive computers
> Submitter: Nicola Larosa
>
> # Filename: HeartbeatClient.py
>
> """Heartbeat client, sends out an UDP packet periodically"""
>
> import socket, time
>
> SERVER_IP = '127.0.0.1'; SERVER_PORT = 43278; BEAT_PERIOD = 5
>
> print ('Sending heartbeat to IP %s , port %d\n'
> 'press Ctrl-C to stop\n') % (SERVER_IP, SERVER_PORT)
> while True:
> hbSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> hbSocket.sendto('PyHB', (SERVER_IP, SERVER_PORT))
> if __debug__: print 'Time: %s' % time.ctime()
> time.sleep(BEAT_PERIOD)


Checkout Socket [1] and Time::HiRes [2], both of which should be
installed already, depending on what version of perl you're working
with. The rest should be fairly simple to translate by following the
code examples. Let us know how that works out for you.

[1] <http://search.cpan.org/~nwclark/per...ocket/Socket.pm>
[2] <http://search.cpan.org/~jhi/Time-HiRes-1.66/HiRes.pm>
Randal L. Schwartz

2004-12-28, 3:56 pm

>>>>> "Andrew" == Andrew Gaffney <agaffney@skylineaero.com> writes:

Andrew> It works for me. That's the way I'd seen it done when I was learning
Andrew> bash. I believe the while checks the return value, not the output of
Andrew> the command.

That's a bash-ism then, not The One True Shell. I was using The One
True Shell in 1980. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
GMane Python

2004-12-29, 3:56 pm

Thank you for the reply to my topic, not BASH wars, but does this look
correct?



use IO::Socket;
use strict;
use Time::HiRes qw( time alarm sleep );

$server_ip = 'localhost';
$server_port = 43278;
$microseconds = 5_000_000;

while ( <> ) {

my $message =
IO::Socket::INET->new(Proto=>"udp",PeerPort=>$server_port,PeerAddr=>$server_
ip)
or die "Can't make UDP socket: $@";
$message->send("PyHB!");

sleep ($microseconds);

}

-Dave

"Dave Gray" <yargevad@gmail.com> wrote in message
news:ed4f42020412280811623c21ab@mail.gmail.com...
Netware[color=darkred]
>
> Checkout Socket [1] and Time::HiRes [2], both of which should be
> installed already, depending on what version of perl you're working
> with. The rest should be fairly simple to translate by following the
> code examples. Let us know how that works out for you.
>
> [1] <http://search.cpan.org/~nwclark/per...ocket/Socket.pm>
> [2] <http://search.cpan.org/~jhi/Time-HiRes-1.66/HiRes.pm>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>




JupiterHost.Net

2004-12-29, 3:56 pm



GMane Python wrote:
> Thank you for the reply to my topic, not BASH wars, but does this look
> correct?
>
>
>
> use IO::Socket;
> use strict;
> use Time::HiRes qw( time alarm sleep );
>
> $server_ip = 'localhost';
> $server_port = 43278;
> $microseconds = 5_000_000;


You need to do

my $server_ip ... etc with use strict;

Did you even try it? or at least perl -c file.pl ?
GMane Python

2004-12-29, 3:56 pm

I'm having a bit of trouble so far. The code below is what I've found on
different web sites as being the functions I believe I need:

use IO::Socket;
use strict;
use Time::HiRes qw( time alarm sleep );

while ( <> ) {

my $message =
IO::Socket::INET->new(Proto=>"udp",PeerPort=>'43278',PeerAddr=>'127.0.0.1')
or die "Can't make UDP socket: $@";
$message->send("PyHB");

sleep (5_000_000);
}

However, it doesn't register on my Python server like the Python script
does. Any idea what I'm doing wrong?

Thanks
-Dave



GMane Python

2004-12-29, 3:56 pm

Yes, but I posted a wrong file. This one doesn't work at all. I have
another that seems to work, but doesn't register on my Python heartbeat
server. It's posted as a reply to Dave just above. Sorry about the wrong
file.

-Dave
"JupiterHost.Net" <mlists@jupiterhost.net> wrote in message
news:41D2F3AF.40904@jupiterhost.net...
>
>
> GMane Python wrote:
>
> You need to do
>
> my $server_ip ... etc with use strict;
>
> Did you even try it? or at least perl -c file.pl ?
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>




GMane Python

2004-12-29, 8:55 pm

It does not compile on Netware, but it's OK on Windows. For Netware, I
copied from my Windows PC the perl/lib and perl/site directories to Netware
because Sockets::IO was not found. Now, I get:

Missing $ on loop variable at sys:\perl\lib/strict.pm li
BEGIN failed--compilation aborted at sys:\perl\lib/IO/So
BEGIN failed--compilation aborted at pyhbclt.pl line 3.

Can anyone point me in a direction here with this error?

Another problem I had but resolved: I thought the sleep was in
microseconds, not seconds. It would work once then not again. Then I
decided I had to test EVERYTHING. Thanks to all those who helped. For
anyone who wants to use PYTHONS ThreadedHeartBeatServer.py by Nicola Larosa
to monitor a Netware Server, the Perl port is here:

#!/usr/bin/perl
use IO::Socket::INET;
use Time::HiRes qw( time alarm sleep );

do
{
my $MySocket=new IO::Socket::INET->new(Proto=>"udp",
PeerPort=>43278,
PeerAddr=>'localhost'
) or die "Can't make UDP socket: $@";
$msg="PyHB";

$MySocket->send($msg);
sleep (5);
}
while (1==1);



Dave Gray

2004-12-29, 8:55 pm

On Wed, 29 Dec 2004 12:50:21 -0500, GMane Python
<s_david_rose@hotmail.com> wrote:
> while ( <> ) {


That isn't doing what you expect, which (I assume) is an infinite
loop. <> loops over @ARGV and attempts to open each arg as a file and
iterate over the lines in each. I suppose it is functionally a
somewhat infinite (heh) loop with no args, as it reads from STDIN
until EOF. Except that it won't do anything until it can read
something from STDIN.

You probably want while (1) { ... } The rest looks ok at first glance,
other than the variable declaration thing.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com