Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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

by stevenrh (Beadle)
on Oct 05, 2004 at 18:35 UTC ( #396709=perlquestion: print w/replies, xml ) Need Help??

stevenrh has asked for the wisdom of the Perl Monks concerning the following question:

Divine Monks, I currently have a file that is a list of HTTP statements. I want open a socket (or a socket for each HTTP statement), and print the http command:
so far, this is my direction...and this may be wrong (hence the post)
I plan to open the file
$file = '/home/steven/http.list'; open(INFO, $file); @lines = <INFO>; close(INFO);

then I was thinking use a "for, foreach"
foreach $i(@lines){

and this is where i need a socket???
I have heard that I could use IO::Socket, but not really knowing the semantics of perl, it's not quite as easy to piece together the scraps of sample code I've borrowed from all corners of the world....
here's my socket :
#!/usr/bin/perl -w use strict; use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => '', PeerPort => '80', Proto =>'tcp', ); die "Could not create socket: $!\n" unless $sock; print $sock ""this is where i need my http statement to go (from the f +ile list"; close($sock);

my green horns are clearly poking through.....
I appreciate any help ..cheers..... stevenrh

UPDATE: Thanks all for your help, and for being so cool to a noob, who could've RTFM (for a while).
I really appreciate it

Replies are listed 'Best First'.
Re: open a socket and print from stdin....i think
by NetWallah (Canon) on Oct 05, 2004 at 18:50 UTC
    Could I gently suggest that you will get yourself into a lot of trouble as a newbie doing Socket programming, and complicating it with a protocol like HTTP ?

    I recommend that you look at some abstractions of the socket that let you view your HTTP request as an HTTP object. Please look at

        Earth first! (We'll rob the other planets later)

Re: open a socket and print from stdin....i think
by ides (Deacon) on Oct 05, 2004 at 18:52 UTC
    I think you should look into the LWP modules. It will simplify this for you greatly by removing all of the networking complexity for you. Here is an example:
    use LWP::Simple; # insert your code to populate @lines here foreach my $url (@lines) { my $content = get($url); }

    Frank Wiles <>

Re: open a socket and print from stdin....i think
by hsinclai (Deacon) on Oct 05, 2004 at 19:58 UTC
    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 = ''; sub get_pagesum { + my $ua = LWP::UserAgent->new( agent => ''); + 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...

      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 ?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://396709]
Approved by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2023-09-29 00:15 GMT
Find Nodes?
    Voting Booth?

    No recent polls found