Search code examples
perldnsiplwplwp-useragent

LWP::UserAgent set ip of requested url so LWP doesn't have to do dns lookup


I'm using LWP::UserAgent to request a lot of page content. I already know the ip of the urls I am requesting so I'd like to be able to specify the ip address of where the url I am requesting is hosted, so that LWP does not have to spend time doing a dns lookup. I've looked through the documentation but haven't found any solutions. Does anyone know of a way to do this? Thanks!


Solution

  • So I found a module that does exactly what I'm looking for: LWP::UserAgent::DNS::Hosts

    Here is an example script that I tested and does what I specified in my question:

    #!/usr/bin/perl 
    use strict;
    use LWP::UserAgent;
    use LWP::UserAgent::DNS::Hosts;
    
    LWP::UserAgent::DNS::Hosts->register_host(
            'www.cpan.org' => '199.15.176.140',
    );
    
    my $ua = LWP::UserAgent->new;
    $ua->timeout(10);
    $ua->env_proxy;
    
    #actually enforces new DNS settings as if they were in /etc/hosts
    LWP::UserAgent::DNS::Hosts->enable_override;
    
    my $response = $ua->get('http://www.cpan.org/');
    
    if ($response->is_success) {
        print $response->decoded_content;  # or whatever
    }
    else {
        die $response->status_line;
    }