• Publicidad

export http_proxy

¿Apenas comienzas con Perl? En este foro podrás encontrar y hacer preguntas básicas de Perl con respuestas aptas a tu nivel.

export http_proxy

Notapor enric73 » 2012-05-14 11:55 @538

Hola, buenas tardes, perleros,

Una consulta relacionada con acceso a Internet.

Resulta que hasta ahora podía acceder a descargarme archivos desde una máquina sin problemas, pero ahora necesito hacerlo utilizando el proxy, definiendo una variable de entorno: export http_proxy=cache.local:3128.

Lo he probado en el siguiente script para que me baje los ficheros gfs de la NOAA, pero no me chuta. También he utilizado --proxy cache.local:3128 dentro de my $host = 'http://nomads.ncep.noaa.gov'; pero no me descarga... ¿Pueden ayudarme? Gracias

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl -w
  2. # ************************************************************************** #
  3. # Script per baixar els fitxers GFS per dominis                              #
  4. # ************************************************************************** #
  5. #!/usr/bin/env perl
  6. use v5.10;
  7. use URI::Escape;
  8. use LWP::Simple;
  9. use DateTime;
  10.  
  11. $Gribs_gfs = '/home/enric/Desktop/';
  12. $Scripts   = '/home/enric/opt/scripts';
  13.  
  14. my $date = DateTime->now->ymd("");
  15.  
  16. mkdir("$Gribs_gfs/${date}00") or warn $!;
  17.  
  18. escriu("Creant carpeta per deixar els fitxers GFS");
  19.  
  20. $data           = '00';
  21. $interval_hours = 3;
  22.  
  23. for my $i ( 0 .. 4 ) {
  24.  
  25.     $_ = sprintf "gfs.t00z.pgrbf%02d.grib2", $i * 3;
  26.     $Gribs_Date = "$Gribs_gfs/${date}00/mygrib$data";
  27.     my $host = 'http://nomads.ncep.noaa.gov';
  28.     my $cgi  = 'cgi-bin/filter_gfs.pl';
  29.     my %args = (
  30.  
  31.         file                  => $_,
  32.         lev_10_m_above_ground => 'on',
  33.         var_UGRD              => 'on',
  34.         var_VGRD              => 'on',
  35.         subregion             => '',
  36.         leftlon               => 10,
  37.         rightlon              => 20,
  38.         toplat                => 25,
  39.         bottomlat             => 15,
  40.         dir                   => "/gfs.${date}00",
  41.     );
  42.  
  43.     my $URL = "$host/$cgi" . '?' . join "&", map { $_ . '=' . uri_escape( $args{$_} ) } keys %args;
  44.  
  45.     say "[$URL]";
  46.  
  47.     mirror( $URL, $Gribs_Date );
  48.  
  49.     $data = sprintf "%02d", $data + $interval_hours;
  50. }
  51.  
  52. {
  53.     $Gribs = "$Gribs_gfs/${date}00";
  54.     chdir("$Gribs");
  55.     print "$Gribs";
  56.     $dates_dat  = '/home/enric/Desktop';
  57.     $Dadesdiari = "$dates_dat/${date}00";
  58.  
  59.     $data = '00';
  60.  
  61.     $interval_hours = 3;
  62.  
  63.     for my $i ( 0 .. 4 ) {
  64.  
  65.         $_ = sprintf "mygrib%02d", $i * 3;
  66.  
  67.         @args = ("wgrib2 $_ -s | grep ':VGRD:10 m above ground' |wgrib2 -i $_ -text $Dadesdiari/VGRD10m$data.dat ");
  68.         system(@args) == 0 or die "system @args failed: $?";
  69.         @args = ("wgrib2 $_ -s| grep ':UGRD:10 m above ground' | wgrib2 -i $_ -text $Dadesdiari/UGRD10m$data.dat ");
  70.         system(@args) == 0 or die "system @args failed: $?";
  71.  
  72.         escriu("arxius dat generats");
  73.  
  74.         $data = sprintf( "%02d", $data + $interval_hours );
  75.     }
  76.  
  77. }
  78. @args = ("$Scripts/vent.pl ${date}00 > $Scripts/vent.dat");
  79. escriu("@args");
  80. system(@args) == 0 or die $!;
  81.  
  82. sub escriu {
  83.     my @missatge = @_;
  84.     chomp( my $hora = `date |awk '{print \$4}'` );
  85.     print "----->  $hora @missatge\n";
  86. }
  87.  
Coloreado en 0.004 segundos, usando GeSHi 1.0.8.4
enric73
Perlero nuevo
Perlero nuevo
 
Mensajes: 154
Registrado: 2012-03-16 06:27 @311

Publicidad

Re: export http_proxy

Notapor enric73 » 2012-05-14 12:34 @565

Posiblemente utilizando,

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use HTTP::Proxy;
  2.  
  3.     # initialisation
  4.     my $proxy = HTTP::Proxy->new( port => 3128 );
  5.  
  6.     # alternate initialisation
  7.     my $proxy = HTTP::Proxy->new;
  8.     $proxy->port( 3128 ); # the classical accessors are here!
  9.  
  10.     # this is a MainLoop-like method
  11.     $proxy->start;
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Puede que esto funcione, ¿no?
enric73
Perlero nuevo
Perlero nuevo
 
Mensajes: 154
Registrado: 2012-03-16 06:27 @311

Re: export http_proxy

Notapor explorer » 2012-05-14 12:48 @575

Una forma sería colocar una línea así

$ENV{'HTTP_PROXY'}='http://cache.local:3128/';

al principio del programa (es también lo mismo si en el shell, haces un export a HTTP_PROXY).

Otra forma sería modificar el LWP::UserAgent para que use ese valor como proxy:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use LWP::Simple qw($ua get);
  2. $ua->proxy('http','http://cache.local:3128');
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


HTTP::Proxy hace el proceso contrario: crea un proxy.
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14480
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: export http_proxy

Notapor enric73 » 2012-05-14 14:00 @625

Gracias explorer,

lo he probado y funciona, gracias de nuevo

saludos
enric73
Perlero nuevo
Perlero nuevo
 
Mensajes: 154
Registrado: 2012-03-16 06:27 @311


Volver a Básico

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 10 invitados

cron