Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: open a socket and print from stdin....i think

by hsinclai (Deacon)
on Oct 05, 2004 at 19:58 UTC ( [id://396746]=note: print w/replies, xml ) Need Help??


in reply to open a socket and print from stdin....i think

As mentioned, LWP is probably much better than what you're trying.. If you want to experiment, you might look at the LWP::UserAgent module.. it is cool because it has great options and you can politely identify yourself. However I found the documentation a bit abstruse.. so below is a quick example that might help..

use LWP; my $finaldata; my $query_url = 'http://www.bla.com/'; sub get_pagesum { + my $ua = LWP::UserAgent->new( agent => 'http://mysite.com/'); + my $response = $ua->get("$query_url", + ':content_cb' => \&gen64, + ); + } + + sub gen64 { + my($data, $response, $protocol) = @_; + return $finaldata .= $data; + } # whatever you got off the last request # should now be contained in $finaldata
(In my case I was sending $finaldata off to be md5 checksummed.)

Of course, if your file contains a list of URLs you must fetch, then you'll be carrying out the procedure for each line in the file...

Replies are listed 'Best First'.
Re^2: open a socket and print from stdin....i think
by rvosa (Curate) on Oct 05, 2004 at 21:58 UTC
    I am working on the assumption that the question is about hosting a socket server that writes out responses with GET requests to whoever logs in (why???). Perhaps I am misunderstanding the original question because the responses suggesting LWP::UserAgent don't seem to address it? Anyway, something like this might work:
    #!/usr/bin/perl # startup with "scriptname localport redirect" use warnings; use strict; use IO::Socket; # port to listen on, stuff to write to clients my ( $localport, $redirect ) = ( $ARGV[0], $ARGV[1] ); # setting up server my $server = IO::Socket::INET->new( LocalPort => $localport, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1 ) || die "couldn't be a tcp server on port $localport:$@\n"; # status print qq{DAEMON LISTENING ON: $localport\n}; # new connections are instantiated like so while ( my ($client, $c_addr) = $server->accept()) { # collecting details about new connection my ($client_port, $c_ip) = sockaddr_in($c_addr); my $client_ipnum = inet_ntoa($c_ip); my $client_host = gethostbyaddr($c_ip, AF_INET); # printing client info print qq{INCOMING: $client_host, $client_ipnum\n}; # retrieving whatever the incoming connection is saying while(<$client>) { print; print $client $redirect; } }
    Not (really) tested.
      Hm.. I think all who mentioned LWP understood the OP to intend to mean
      open a socket to a remote server in order to execute the "command" found in a file

      stevenrh ?

        On second thought, I think that's what it meant too. Oops.
        Yes. I have a list of links (with session ID, etc) i need to hit on a website. Contrary to my first post, I actually need to issue the POST command.
        example..I can:

        nc -vvv website.org 80

        and then put in my command:

        POST http://website.org/action2?sessionid /HTTP/1.0

        then i hit return a few times...and i get the html output.
        BUT
        as most have pointed out, I think LWP is the easiest, and best way, especially for me..

        also note, the "http://website.org/action2?sessionid" is coming from an array, because it's a bunch of links in a file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://396746]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-25 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found