in reply to compare server response?
In this example a web connection sends a "Connection: close" which gets the server to hangup - thus flushing the output.
If you were connecting to an FTP server for example you would send "QUIT\n"#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $server = "remote.server.net"; my $port = 43; my $path = "/"; my $socket=IO::Socket::INET->new( PeerAddr => $server, PeerPort => $port, Proto=> 'tcp') || die "Can't connect to $server:$port: $!\n"; my $data = qq~GET $path HTTP/1.1 Host: $server Accept: */* User-Agent: My Perl Script Pragma: no-cache Cache-Control: no-cache Connection: close ~; print $socket "$data\r\n"; my $body = 0; while(<$socket>) { my $line = $_; if ($body eq 1) { print $line; } $body = 1 if ($line=~/Connection: close/i); } close($socket);
If you're reading the data in a while loop and then sending something which is hanging up the connection when you see what you want:
it can sometimes make a difference if you put:#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $remote_server = "ftp.server.net"; my $remote_port = 21; my $sock = IO::Socket::INET->new("$remote_server:$remote_port") || die + $!; my $data = "HELP"; my $string_to_compare = "^220 "; print $sock "$data\r\n"; my @output; $| = 1; while (<$sock>) { push(@output, $_); print $sock "QUIT\n" if /$string_to_compare/gi; } close($sock); my $output = join('', @output); sub true { print "Ain't dat da true! :o)\n"; # open(tmpl, "email.txt") || print $!; } sub false { print "Close, but no banana.\n"; } if ($output =~ /$string_to_compare/gi) { &true; } else { &false; }
Just before the while loop, which turns off buffering - thus auto-flushing the response from the server (see perlvar and look for $OUTPUT_AUTOFLUSH ) - but in your example the line$| = 1;
is just going to sit there forever waiting for an EOF and the program will never get to themy @output = <$sock>;
to carry on with the rest of the logic.$output = join('', @output);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: compare server response?
by webshark (Acolyte) on Dec 07, 2005 at 17:37 UTC |