Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Problems with ftp scripts

by LiTinOveWeedle (Scribe)
on May 11, 2001 at 21:51 UTC ( [id://79786]=perlquestion: print w/replies, xml ) Need Help??

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

Hi brothers,
I try to make my ftp transfer script functional, but I really must have somewhere in my code big error. My script should be started from web, It can run in two modes first "generate" only generate html page from template in which is inserted log from whole scripts. This part running well, so we can focus on second function - "transfer".
Scripts at first look into temp file to see, if not already running. If not, look into directory for files to transfer. If there are some files it connect to ftp server and should be transferall files. Why do I sayd should? Because if file is bigger ( I try 1MB and more ) script transfer only part of file.

OK if you know where I did mistake plz help me. Also plz be friendly to my code, this is only for testing....

code is inside

THX to all.

Li Tin O've Weedle
mad Tsort's philosopher

Replies are listed 'Best First'.
Re: Problems with ftp scripts
by AgentM (Curate) on May 11, 2001 at 23:51 UTC
    Since the problem involves a server timeout which you have problems getting around, I suggest that you use system calls. Under normal circumstances, of course, this is hardly "good", but since you really need the power of fork under windows (if I understand correctly), then your best bet would be to interface with a shell-based FTP client. This might involve writing to a temporary file to upload, but it circumvents the timeout on the scripts (perhaps a config option for this?) and will get the job done with minimum hassle. The script looks good and, as you say, it works fine until a timeout, so you've probably nailed the problem alot of other people have had in FTP via web. Good luck!
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
      THX AgentM,
      I think so, that problem is in forking into separate proces. And you'r right I running script under winnt4 an win9x platform. I use same technique which merlyn describe in his Web Techniques Column 20. But no succes.

      Anybody had same problem with forking under win32?
      Whole problem is that my webserver probably terminate both child and parent processes, so ftp transfer is interupted before it can end.

      Any suggestions? I will be glad if there can be other way than using ext. ftp program.
      I will try to separate script into two independent scripts and start ftp script by system call. This should work but this isn't purist solution.

      Li Tin O've Weedle
      mad Tsort's philosopher

Re: Problems with ftp scripts
by LiTinOveWeedle (Scribe) on May 11, 2001 at 21:55 UTC
    #!/usr/local/bin/perl # -------------------------------------------------------------------- +----- #global config $local_dir = "C:/Temp/"; $transfered_dir = "C:/Temp/Transfered/"; @file_types = ("\.doc", "\.pdf", "\.prn", "\.ps"); $ftp_site = "ftp.domain.com"; $ftp_timeout = 30; $ftp_retry = 3; $ftp_user = "test"; $ftp_pass = "1234"; $log = "1"; $log_file_path = "logs/ftp.log"; $file_lock = ""; $show_logs_lines = 20; $ok_redirect_to = "modules/templates/template4.html"; $ko_redirect_to = "modules/templates/template3.html"; $run_redirect_to = "modules/templates/template2.html"; $indicator_file_path = "logs/ftptransfer.tmp"; $ftp_template[0] = "modules/templates/template1.html"; # -------------------------------------------------------------------- +----- # main program body - no changes needed under this line use Net::FTP; use CGI; $|++; $query = new CGI; if ( $query -> param('method') eq "generate" ) { &ftp_generate; } elsif ( $query -> param('method') eq "transfer" ) { &ftp_transfer; } else { die; } # -------------------------------------------------------------------- +----- sub ftp_generate { $html_template_path = $ftp_template[$query -> param('template')]; if ( $html_template_path eq "" ) { $html_template_path = $ftp_temp +late[0]; } &ftp_read_html; $ftp_html =~ s/<<log>>/&ftp_read_log/isge; $ftp_html =~ s/<<dir>>/&ftp_read_dir/isge; print $ftp_html."\n"; } sub ftp_transfer { if ( &ftp_read_indicator ) { &ftp_write_indicator(1); &ftp_logging("#Transfer script started - from web"); if ( &ftp_read_file ) { if ( fork() ) { print $query->redirect( -uri => $ok_redirect_to ); exit; } else { close STDIN; close STDOUT; if ( &ftp_connect_to ) { &ftp_transfer_file; } } } else { &ftp_logging("No files to transfer!"); print $query->redirect( -uri => $ko_redirect_to ); } &ftp_write_indicator(0); } } # -------------------------------------------------------------------- +----- # read file in dir and filter files to transfer sub ftp_read_file { my ( $type, $name, $temp, @temp ); if ( not opendir(DIR, $local_dir) ) { &ftp_logging("Can't open local dir"); die; } @temp = readdir(DIR); closedir(DIR); foreach $name ( sort @temp ) { foreach $type ( @file_types ) { $temp = $local_dir . $name; if ( $name =~ /^[^\.]*($type)$/is and -f $temp ) { push( @files, $name ); } } } $temp = @files; if ( $temp != 0 ) { return 1; } else { return 0; } } # -------------------------------------------------------------------- +----- # logging sub ftp_logging { my ( @time, $i ); if ( $log ne "" ) { if ( not open(LOG, ">>".$log_file_path) ) { die; } @time = localtime(time); $time[4] = $time[4] + 1; $time[5] = $time[5] + 1900; for ( $i=0; $i<@time; $i++ ) { if ( $time[$i] < 10 ) { $time[$i] = "0" . $time[$i]; } } if ( $file_lock ) { flock(LOG, 2); } print LOG "$time[3]/$time[4]/$time[5] $time[2]:$time[1]:$time[ +0] \t @_ \r\n"; close(LOG); } } # -------------------------------------------------------------------- +----- # conect to ftp server sub ftp_connect_to { if ( $ftp = Net::FTP->new($ftp_site) ) { &ftp_logging("Conected to $ftp_site"); } else { &ftp_logging("Can't connect to $ftp_site"); return 0; } if ( $ftp->login($ftp_user, $ftp_pass) and $ftp->binary() ) { &ftp_logging("Authentificated, transfer mode set"); } else { return 0; } return 1; } # -------------------------------------------------------------------- +----- # ftp transfer file sub ftp_transfer_file { my ( $name, $counter, $attemp, $error ); $counter = 0; &ftp_logging("Transfering files..."); foreach $name ( @files ) { $error = 1; $attemp = 1; while ( $attemp <= $ftp_retry ) { if ( $ftp->put( $local_dir. $name ) and &ftp_sum_check($name) +) { $attemp = $ftp_retry + 1; $counter++; if ( not -d "$transfered_dir" ) { mkdir( $transfered_dir ); } rename ( $local_dir. $name, $transfered_dir. $name ); &ftp_logging("$name transfered"); } else { &ftp_logging("$name not transfered - attemp $attemp"); $attemp++; $ftp->quit; sleep 10; &ftp_connect_to; } } } $ftp->quit; &ftp_logging("Transfer complete, $counter file transfered"); } # -------------------------------------------------------------------- +----- # check transfered file sub ftp_sum_check { my ( $name, @file ); $name = @_[0]; @file = stat( $local_dir. $name ); if ( $ftp->size($name) == $file[7] ) { &ftp_logging("Control size of file $name validated - $file[7] byte +s"); return 1; } else { &ftp_logging("File $name has bad control size - should be $file[7] + bytes"); return 0; } } # -------------------------------------------------------------------- +----- # read data from log sub ftp_read_log { my ( $line, $temp, $temp1, $html ); if ( not open(FILE, $log_file_path) ) { &ftp_logging( "Can't open log file." ); die; } if ( $file_lock ) { flock(FILE, 1); } @logdata = (); while ( $line = <FILE> ) { chomp($line); push(@logdata, $line); } close(FILE); if ( $query->param('log') > $show_logs_lines ) { $show_logs_lines += $query->param('log'); } $temp = @logdata; if ( $temp > $show_logs_lines ) { $temp = $temp - $show_logs_lines +; } splice ( @logdata, 1, $temp ); foreach $line ( @logdata ) { $html .= $line."<br>"; } return $html; } # -------------------------------------------------------------------- +----- # read file in dir and filter files to transfer sub ftp_read_dir { my ( $type, $name, $temp, @temp, $html ); $html .= "<b>Files to transfer:</b><br>"; if ( not opendir(DIR, $local_dir) ) { $html .= "nemohu otevrit adr +esar<br>"; } @temp = readdir(DIR); closedir(DIR); foreach $name ( sort @temp ) { $temp = $local_dir . $name; if ( -f $temp ) { $html .= $name."<br>"; } } $html .= "<p><b>Transfer files:</b><br>"; if ( not opendir(DIR, $transfered_dir) ) { $html .= "nemohu otevri +t adresar<br>"; } @temp = readdir(DIR); closedir(DIR); foreach $name ( sort @temp ) { $temp = $transfered_dir . $name; if ( -f $temp ) { $html .= $name."<br>"; } } return $html; } # -------------------------------------------------------------------- +----- # read indicatot file and check if scripts allready not running sub ftp_read_indicator { my ( $line ); if ( not open(IND, $indicator_file_path) ) { &ftp_logging( "Can't open indicator file." ); die; } if ( $file_lock ) { flock(IND, 1); } @logdata = (); $line = <IND>; close(IND); if ( $line eq "0" ) { return 1; } else { return 0; } } # -------------------------------------------------------------------- +----- # write indicator to file sub ftp_write_indicator { my ( $line ) = @_[0]; if ( not open(IND, ">".$indicator_file_path) ) { &ftp_logging( "Can't open indicator file." ); die; } if ( $file_lock ) { flock(IND, 2); } print IND "$line"; close(IND); } # -------------------------------------------------------------------- +----- # read html template file sub ftp_read_html { my ( @html ); if ( open(HTML, $html_template_path) ){ @html=<HTML>; close(HTML); } else { &ftp_logging( "Can't open html read template file. file wasn't fou +nd at $html_template_path." ); die; } $ftp_html = join( "", @html ); }

    I made some changes before posting, hope that code is fine.

    Li Tin O've Weedle
    mad Tsort's philosopher

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found