Building a Simple HTTP Proxy in Ruby
Ruby makes it easy to write network applications with its socket library and HTTP libraries like net/http.
First we require the needed libraries:
require 'socket'
require 'net/http'
Next we create a TCPServer to listen for connections:
server = TCPServer.new('localhost', 3000)
Then we loop through each connection:
while (connection = server.accept)
# handle request
end
To handle each request we:
request = connection.read_nonblock(256)
url = request.split[1]
response = Net::HTTP.get_response(URI(url))
connection.puts response
The full script:
require 'socket'
require 'net/http'
server = TCPServer.new('localhost', 3000)
while (connection = server.accept)
request = connection.read_nonblock(256)
url = request.split[1]
response = Net::HTTP.get_response(URI(url))
connection.puts response
end
This gives you a very simple HTTP proxy in Ruby in just a few lines. Many improvements could be made but it demonstrates the basic approach.
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.
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.
Related articles:
- How to Build a Super Simple HTTP proxy in Go in just 20 lines of code
- How to Build a Super Simple HTTP Proxy in Objective-C in Just 14 Lines of Code
- How to Build a Super Simple HTTP Proxy in Perl in just 20 lines of code
- How to Build a Simple HTTP Proxy in Rust in just 40 lines
- How to Build a Super Simple HTTP Proxy in C++ in just 30 lines of code
- Scraping Craigslist Listings with Ruby
- Serving HTTP Requests Efficiently with aiohttp's TCPServer
Browse by tags:
Browse by language:
Popular articles:
- Web Scraping in Python - The Complete Guide
- Working with Query Parameters in Python Requests
- How to Authenticate with Bearer Tokens in Python Requests
- Building a Simple Proxy Rotator with Kotlin and Jsoup
- The Complete BeautifulSoup Cheatsheet with Examples
- The Complete Playwright Cheatsheet
- Web Scraping using ChatGPT - Complete Guide with Examples