IO::Socket and LWP::UserAgent
JP Howard
jh_lists at fastmail.fm
Thu Nov 21 02:46:13 CST 2002
On Thu, 21 Nov 2002 07:38:02 +0000, "Piers Harding" <piers at ompa.net>
said:
> While LWP is a magnificent module - it is slow.
>
> Something that you might consider is writing a specific C extension to
> handle your HTTP/HTTPS connections. There is one that uses Gnomes HTTP
> module ( written by Matt Sargent ) HTTP::GHTTP, but I don't think that
> this supports SSL. If all you need to do is GETs then VFS::Gnome ( soon
> to be part of VFS ) will do it, or CURL::easy, but if you need to POST
> then that is a problem.
>
A simpler approach would simply be to use Parallel::ForkManager. This
module lets you maintain a pool of n clients working together. It neatly
gets over the latency problem when writing LWP clients. Here's a
template:
----
my $PFM = Parallel::ForkManager->new(25);
for (1..10) {
$PFM->start and next;
do_something();
$PFM->finish;
}
$PFM->wait_all_children;
print "all done!\n";
----
Just write your sub do_something{} and you're done!
>
> On Thu, Nov 21, 2002 at 05:16:58PM +1100, tim wrote:
> > Hello.
> >
> > I'm trying to connect to a service provider using two methods:
> >
> > HTTPS:// GET via LWP::UserAgent;
> > TCP/IP stream via IO::Socket::SSL;
> >
> > In each case I'm sending information to be SMSed to mobile phones.
> >
> > My script instantiates an object and then calls the specific ->send()
> > method; in each case the object is reused. The HTTPS method is about
> > three times faster than the socket method. The socket seems to put one
> > message per second, whilst the supplier has suggested that 20 per second
> > is achievable.
> >
> > Am I missing something intrinsic about IO::Socket::SSL (and IO::Socket
> > in general), or shoudl I be talking to the provider to get their system
> > tuned? They use Win NT as their server, and that's about all I know :)
> >
> > Thanks in advance,
> >
> > Tim.
> >
> > ----------
> >
> > [SCRIPT]
> > my $sms = new Monash::SMS_Stunnel($account, $pass);
> > my $ssl_mobile_number = '0419xxxxxxxxx';
> > my $message = 'TEXT for test purposes';
> > my $str;
> > map {
> > $response = $sms->send_socket_SSL($ssl_mobile_number, $message);
> > if ($response) {
> > $str = "Message $_ sent from process $$";
> > } else {
> > $str = "Message $_ failed from process $$";
> > }
> > carp $str;
> > } (1..200);
> >
> > [MODULE]
> >
> > sub new
> > {
> > use IO::Socket::SSL;
> > my $remote_ssl = IO::Socket::SSL->new(
> > Proto=> 'tcp',
> > PeerAddr => 'xx.xx.xx.xx',
> > PeerPort => 'xxx',
> > )
> > or die "cannot connect to [SSL] server";
> >
> > my ($clazz, $account_name, $passwd) = @_;
> >
> > my $self = {
> > _account_name => $account_name,
> > _passwd => $passwd,
> > _remote_ssl => $remote_ssl,
> > };
> >
> > bless $self, $clazz;
> > return $self;
> > }
> >
> > sub send_socket_SSL($)
> > {
> > my ($self,$mobile_number,$message) = @_;
> >
> > # These three should form part of the object invocation:
> > my $sender = 'xxxxxx';
> > my $sender_address = 'xxxxxxxxxxxxxx';
> > my $recipient = 'xxxxxxxxxxxxx';
> > my $remote = $self->{_remote_ssl};
> > # Okay, let's send it!
> > print $remote qq{\r\r}.$self->{_account_name}.qq{\r1\r1\r}.
> > qq{$sender\r}.
> > qq{$sender_address\r}.
> > qq{$recipient\r}.
> > qq{$mobile_number}.
> > qq{\rGSM}.
> > qq{\r0\r0\r0\r}.$self->{_passwd}.qq{\r}.
> > qq{$message};
> >
> > # Send result
> > # Accepted Result code = 0x06 Description = Lodge OK
> > # Rejected Result code = 0x15 Description = Lodge Error
> >
> > my $strbuff = '';
> > $remote->read($strbuff,10,0);
> > # Next line for debug purposes only:
> > # print "\nstrbuff was ($strbuff)\n";
> > if ($strbuff =~ /Lodge OK/) {
> > return 1;
> > } else {
> > return 0;
> > }
> > }
> >
> > __END__
>
More information about the Melbourne-pm
mailing list