use LWP::Simple to get ftp site content in Perl
I am having some issue here using LWP::Simple to get ftp site content in Perl. It would work fine if the site used http, but not ftp, for example ftp://ftp.di.uminho.pt/pub/ctan/fonts/. But in my firefox browser, I can view the content as html.
So how 开发者_StackOverflow社区to get the html of the site in this case?
LWP returns a document of the type text/ftp-dir-listing
for a FTP directory. Use File::Listing to parse it.
$ GET -USe ftp://ftp.di.uminho.pt/pub/ctan/fonts/
GET ftp://ftp.di.uminho.pt/pub/ctan/fonts/
User-Agent: lwp-request/6.00 libwww-perl/6.02
200 OK
Server: --------- Welcome to Pure-FTPd [privsep] ----------
Content-Length: 32208
Content-Type: text/ftp-dir-listing
Client-Date: Mon, 06 Jun 2011 21:32:45 GMT
Client-Request-Num: 1
drwxr-xr-x 257 500 50 20480 May 30 06:27 .
drwxrwsr-x 18 500 50 4096 Jun 6 20:02 ..
drwxr-xr-x 2 500 500 4096 Apr 7 19:13 Asana-Math
-rw-r--r-- 1 500 500 573482 Apr 7 19:14 Asana-Math.zip
drwxr-xr-x 2 500 50 4096 May 20 2005 CJK
-r--r--r-- 1 500 500 20384230 May 20 2005 CJK.zip
drwxr-xr-x 2 500 50 4096 May 20 2005 DayRoman
-r--r--r-- 1 500 500 573352 May 20 2005 DayRoman.zip
drwxr-xr-x 2 500 50 4096 Sep 7 2007 LuxiMono
-r--r--r-- 1 500 500 199660 May 6 2005 LuxiMono.zip
lrwxrwxrwx 1 500 50 8 Oct 14 2005 MnSymbol -> mnsymbol
lrwxrwxrwx 1 500 50 12 Oct 14 2005 Mnsymbol.zip -> mnsymbol.zip
['Asana-Math', 'd', undef, 1302196380, 16877],
['Asana-Math.zip', 'f', 573482, 1302196440, 33188],
['CJK', 'd', undef, 1116540000, 16877],
['CJK.zip', 'f', 20384230, 1116540000, 33060],
['DayRoman', 'd', undef, 1116540000, 16877],
['DayRoman.zip', 'f', 573352, 1116540000, 33060],
['LuxiMono', 'd', undef, 1189116000, 16877],
['LuxiMono.zip', 'f', 199660, 1115330400, 33060],
['MnSymbol', 'l mnsymbol', 8, 1129240800, 41471],
['Mnsymbol.zip', 'l mnsymbol.zip', 12, 1129240800, 41471],
You probably want to use Net::FTP
instead:
use Net::FTP;
$ftp = Net::FTP->new("some.host.name", Debug => 0)
or die "Cannot connect to some.host.name: $@";
$ftp->cwd("/pub")
or die "Cannot change working directory ", $ftp->message;
$ftp->get("that.file")
or die "get failed ", $ftp->message;
$ftp->quit;
Firefox will internally generate an HTML document when you are viewing an FTP directory. The FTP server won't provide one.
If you want one, then you will have to iterate over the files and directories and generate your own.
精彩评论