Perl Configuration for the Proxy

This article describes how to configure the Proxy Host with Basic Authentication in the Perl programming/scripting language for an HTTP client.

ProxyMesh also supports IP address authentication, for which you do not need to use a username and password. Instead, set the http_proxy environment variable to http://PROXYHOST:PORT.

Perl Library for the Web

Perl libraries are available through the CPAN (for " Comprehensive Perl Archive Network"). Among CPAN libraries for making HTTP requests, LWP (for "libwww-perl") is a popular choice. Included in LWP is an API to an HTTP client as well as a range of HTML utilities and standard objects to represent HTTP requests and responses.

Here's a code example for a request from LWP 6.39 ( from https://metacpan.org):

	#!/usr/bin/env perl
        use strict;
        use warnings;
        use feature qw( say );

        use LWP::UserAgent;
	my$ua= LWP::UserAgent->new;

	my@phases= (
	    'request_preprepare', 'request_prepare',
	    'request_send',       'response_header',
	    'response_data',      'response_done',
	    'response_redirect',
	);
       formy$phase(@phases) {
	$ua->add_handler($phase = > sub { say "$phase"; return undef;});
	}

$ua->get('http://example.com');

Proxy Attributes

The following methods are set up for requests to be passed through a proxy server.

env_proxy

$ua->env_proxy; must be called to load the environmental variable, as described at this link.

http_proxy

This is the environmental variable from which the proxy settings are loaded. The value should look like this: http://PROXYHOST:PORT

Note that HTTP_PROXY is not honored for CGI scripts. Instead, you can use the CGI_HTTP_PROXY environment variable.

CSH or TCSH users should use the setenv command to define these environment variables.

proxy function

from https://github.com/libwww-perl/libwww-perl#proxy. Note thatUSERNAME:PASSWORD is unnecessary if using IP address authentication.

# For a single scheme:
$ua->proxy($scheme, $proxy_url)
$ua->proxy('http', 'http://PROXYHOST:PORT', proxyuserpwd: 'USERNAME:PASSWORD');

# To set multiple proxies at once:
$ua->proxy([
    [ 'http', 'https' ] => 'http://PROXYHOST:PORT',
]);

Perl and libcurl

If you want to extend Perl with cURL/libcurl functionality, you can use WWW::Curl.

Perl and Selenium

Follow this link for information on using Perl with Selenium the automated web UI testing application.

Still need help? Contact Us Contact Us