How to Build a Super Simple HTTP Proxy in Perl in just 20 lines of code

Oct 1, 2023 ยท 3 min read

Perl is a great language for writing network applications thanks to its strong text processing capabilities and support for sockets. In this post we will build a basic HTTP proxy server in Perl in less than 20 lines of code.

First we load the required modules:

use strict;
use warnings;
use IO::Socket::INET;
use LWP::UserAgent;

The IO::Socket::INET module will allow us to create TCP sockets and listen for connections. The LWP::UserAgent module makes it easy to fetch web pages.

Next we create a socket and bind it to port 8080 to listen for incoming connections:

my $socket = IO::Socket::INET->new(
  LocalHost => '127.0.0.1',
  LocalPort => '8080',
  Proto => 'tcp',
  Listen => 1,
  Reuse => 1
) or die "Cannot create socket: $!\\n";

We enable socket reuse and start listening for connections.

Then in an infinite loop we wait for a client to connect, read the HTTP request, extract the URL, fetch the web page, and return the response:

while (my $client = $socket->accept()) {

  my $request = <$client>;
  my $url = parse_url($request);

  my $ua = LWP::UserAgent->new;
  my $response = $ua->get($url);

  print $client $response->content;

  close $client;
}

The parse_url function extracts the URL from the HTTP request:

sub parse_url {
  my $request = shift;
  return (split / /, $request)[1];
}

We split the request string on spaces and return the URL portion.

The full proxy code is:

use strict;
use warnings;
use IO::Socket::INET;
use LWP::UserAgent;

my $socket = IO::Socket::INET->new(
  LocalHost => '127.0.0.1',
  LocalPort => '8080',
  Proto => 'tcp',
  Listen => 1,
  Reuse => 1
) or die "Cannot create socket: $!\\n";

sub parse_url {
  my $request = shift;
  return (split / /, $request)[1];
}

while (my $client = $socket->accept()) {

  my $request = <$client>;
  my $url = parse_url($request);

  my $ua = LWP::UserAgent->new;
  my $response = $ua->get($url);

  print $client $response->content;

  close $client;
}

This demonstrates how to build a simple HTTP proxy server in Perl using less than 20 lines of code.

This is great as a learning exercise but it is easy to see that even the proxy server itself is prone to get blocked as it uses a single IP. In this scenario where you may want a proxy that handles thousands of fetches every day using a professional rotating proxy service to rotate IPs is almost a must.

Otherwise, you tend to get IP blocked a lot by automatic location, usage, and bot detection algorithms.

Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.

  • With millions of high speed rotating proxies located all over the world,
  • With our automatic IP rotation
  • With our automatic User-Agent-String rotation (which simulates requests from different, valid web browsers and web browser versions)
  • With our automatic CAPTCHA solving technology,
  • Hundreds of our customers have successfully solved the headache of IP blocks with a simple API.

    The whole thing can be accessed by a simple API like below in any programming language.

    In fact, you don't even have to take the pain of loading Puppeteer as we render Javascript behind the scenes and you can just get the data and parse it any language like Node, Puppeteer or PHP or using any framework like Scrapy or Nutch. In all these cases you can just call the URL with render support like so:

    curl "<http://api.proxiesapi.com/?key=API_KEY&render=true&url=https://example.com>"
    
    

    We have a running offer of 1000 API calls completely free. Register and get your free API Key.

    Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: