CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Open File on another server

 



Kaleste
stranger

Apr 14, 2001, 3:03 AM

Post #1 of 4 (271 views)
Open File on another server Can't Post

How can I open a file from my script that is on another server?



sleuth
Enthusiast

Apr 16, 2001, 1:06 AM

Post #2 of 4 (255 views)
Re: Open File on another server [In reply to] Can't Post

 
HI Mate,

Well, do you want to common way,

use LWP::Simple;
$file = get("http://www.server.com/file.ext");

Or the more dependable way?

use IO::Socket;
$method = "GET";
$host = "www.server.com";
$path = "/path/to/file.ext";
$socket = new IO::Socket::INET( PeerAddr => $host,
PeerPort => 80,
Proto => 'tcp',
timeout => '2',
Type => SOCK_STREAM, ) or print "$!";
print $socket "$method $path HTTP/1.0\nReferer: $host\n";
print $socket "User-Agent: $ENV{'HTTP_USER_AGENT'}\n\n";
@output = <$socket>;
close ($socket);
foreach $n (0..9){shift(@output);}

Now, your file would be in $output,

Most people use LWP, but when I'm opening data bases that I have to loop through I use the socket code, the LWP module will get the file, but you can't loop through the data, you'd have to write it to a file then read it and then you could loop through it.

If you're getting an image/binary file I'd use LWP, but for data bases that you need to read instantly, sockets is a better way to go.

Hope this is good,

Tony



localfilmmaker
stranger

Apr 17, 2001, 12:18 PM

Post #3 of 4 (241 views)
Re: Open File on another server [In reply to] Can't Post

I agree with your choices. LWP is one of my usual ways of doing this sort of thing. But I'm concerned with your last line in the socket code:
foreach $n (0..9){shift(@output);}

I assume this is to get rid of the HTTP headers returned. If this is the case, then a much better way would be:
1 while (shift(@output) ne "\r\n"); # \r\n is the standard to mark the end of HTTP headers
or
1 while (shift(@output) !~ /^\r?\n$/); # Some web servers don't follow the standard

This will shift off the array until the end of the HTTP headers. If you were trying to do something else, then I guess I missed it.

-localfilmmaker
Spencer Christensen
spencer@mecworks.com


japhy
Enthusiast

Apr 19, 2001, 9:12 AM

Post #4 of 4 (229 views)
Re: Open File on another server [In reply to] Can't Post

I wouldn't say hand-rolling your own is "more dependable". LWP::Simple can handle things like redirects -- IO::Socket doesn't do that on its own, you'd have to make a special case and follow the link provided.

I wrote a module (that uses IO::Socket in the very way I have said it should not be used, just now) to allow you to read from a web page as if it were a regular filehandle, but I haven't released it because it's not very robust.

I think, for most purposes, using LWP::Simple is fine:


Code
use LWP::Simple; 

my $file = get "http://www.pobox.com/~japhy/";

while (my ($line) = $file =~ /(.*)/g) {
# do something which each line of the file
}

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives