Shifter와 함께 사용 Ruby
Shifter의 레지덴셜 및 ISP 프록시를 몇 분 안에 Ruby에 연결하세요. Net::HTTP, HTTParty, Faraday, Mechanize, Watir와 호환되며 일반 스크립트, Sinatra, Rails에서 작동합니다.
빠른 시작
설치
gem install httparty 기본 사용법
require 'httparty'
response = HTTParty.get(
'https://ipinfo.io/json',
http_proxyaddr: 'p.shifter.io',
http_proxyport: 443,
http_proxyuser: 'customer-USERNAME-country-us-sid-123ABC',
http_proxypass: 'PASSWORD',
timeout: 30
)
puts response.parsed_response
# {"ip" => "154.16.xxx.xxx", "city" => "New York", "country" => "US", ...} 기능
예시
Net::HTTP (의존성 없음)
Ruby 표준 라이브러리만으로 충분합니다. Net::HTTP.start는 프록시 호스트, 포트, 사용자, 비밀번호를 받아들이며, 별도의 gem이 필요하지 않습니다. 작은 스크립트와 Lambda 스타일 함수에 가장 적합합니다.
require 'net/http'
require 'uri'
require 'json'
PROXY_HOST = 'p.shifter.io'
PROXY_PORT = 443
PROXY_USER = 'customer-USERNAME-country-uk-sid-456DEF'
PROXY_PASS = 'PASSWORD'
uri = URI('https://example.co.uk/products')
http = Net::HTTP.new(
uri.host,
uri.port,
PROXY_HOST,
PROXY_PORT,
PROXY_USER,
PROXY_PASS
)
http.use_ssl = true
http.read_timeout = 30
req = Net::HTTP::Get.new(uri.request_uri)
req['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
response = http.request(req)
puts "Status: #{response.code}, length: #{response.body.length}" Sticky Session을 사용하는 Faraday
Faraday는 Ruby에서 가장 유연한 HTTP 클라이언트로, 미들웨어 기반이며 어댑터를 자유롭게 교체할 수 있습니다. 프록시 사용자 이름에 `sid`를 추가하면 전체 대화 동안 하나의 레지덴셜 IP를 고정할 수 있습니다.
require 'faraday'
require 'securerandom'
sid = SecureRandom.hex(4)
conn = Faraday.new(
url: 'https://example.de',
proxy: "customer-USERNAME-country-de-city-berlin-sid-#{sid}-ttl-300:PASSWORD@p.shifter.io:443",
request: { timeout: 30 },
headers: { 'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36' }
)
login = conn.post('/login', { email: 'user@example.com', password: 'secret' }.to_json,
'Content-Type' => 'application/json')
dashboard = conn.get('/dashboard')
orders = conn.get('/orders')
puts [login.status, dashboard.status, orders.status].inspect Mechanize (폼 인식 크롤링)
Mechanize는 폼 제출과 링크 추적을 자동화합니다. 에이전트에 프록시를 설정하면 방문하는 모든 페이지가 Shifter를 통과합니다.
require 'mechanize'
agent = Mechanize.new
agent.set_proxy('p.shifter.io', 443,
'customer-USERNAME-country-us-city-newyork-sid-789GHI',
'PASSWORD')
agent.user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
agent.read_timeout = 30
page = agent.get('https://example.com/login')
form = page.form('login')
form.email = 'user@example.com'
form.password = 'secret'
dashboard = form.submit
dashboard.links_with(href: %r{/orders/\d+}).each do |link|
order = link.click
puts order.search('h1').text.strip
end Rails (프로덕션 HTTP 서비스)
Shifter를 작은 Rails 서비스로 감싸서 모든 컨트롤러 / 작업이 동일한 프록시 구성을 사용하도록 하십시오. 요청별로 로테이션하거나 사용자별로 고정할 수 있습니다.
# app/services/shifter_client.rb
require 'faraday'
require 'faraday/retry'
class ShifterClient
PROXY_HOST = 'p.shifter.io:443'
def initialize(country: 'us', sid: nil)
sid_part = sid ? "-sid-#{sid}" : ''
proxy_url = "customer-USERNAME-country-#{country}#{sid_part}:PASSWORD@#{PROXY_HOST}"
@conn = Faraday.new(proxy: proxy_url, request: { timeout: 30 }) do |f|
f.request :retry, max: 3, interval: 1.0, backoff_factor: 2
f.response :json
f.adapter Faraday.default_adapter
end
end
def get(url)
@conn.get(url)
end
end
# In a controller or job:
class ProductsController < ApplicationController
def index
client = ShifterClient.new(country: 'uk', sid: current_user.id)
@products = client.get('https://example.co.uk/products.json').body
end
end 자주 묻는 질문
Ruby와 Shifter 사용에 관한 일반적인 질문.
프록시 호스트, 포트, 사용자, 비밀번호를 Net::HTTP.new의 3번째~6번째 인자로 전달하세요. HTTPS 대상의 경우 use_ssl = true로 설정하세요. Ruby 표준 라이브러리만으로 충분하며 추가 gem이 필요하지 않습니다.
http_proxyaddr, http_proxyport, http_proxyuser, http_proxypass를 모든 HTTParty.get / post 호출의 옵션으로 전달하세요. 또는 클래스 수준의 default_options에 포함시켜 해당 클래스가 만드는 모든 요청이 Shifter를 사용하도록 하세요.
Yes. Pass a `proxy` option when constructing the Faraday connection. The format is `http://USER:PASS@p.shifter.io:443`. Faraday's middleware stack — retry, JSON parsing, logging — composes on top.
프록시 사용자 이름에 세션 ID를 추가하세요(예: `customer-USERNAME-country-us-sid-123ABC`). 동일한 프록시 URL을 사용하는 모든 Ruby 요청은 동일한 레지덴셜 IP를 재사용합니다. `ttl-N`을 추가하면 해당 IP를 최대 N초 동안 고정할 수 있습니다.
예. 프록시를 서비스 클래스(또는 이니셜라이저의 Faraday 연결)로 감싸서 컨트롤러와 ActiveJob 워커 전체에서 재사용하십시오. 동일한 프록시 URL이 동기 및 비동기(Sidekiq) 컨텍스트에서 모두 작동합니다.
gem이 필요하지 않습니다. Shifter는 일반 HTTP / SOCKS5를 사용하므로, 기존 클라이언트를 `p.shifter.io:443`으로 지정하도록 설정하면 완료됩니다. 이렇게 하면 Gemfile이 간결해지고 버전 결합을 피할 수 있습니다.
Shifter 사용을 함께 시작하기 Ruby
5분 이내에 Shifter의 205M+ 레지덴셜 및 ISP 프록시를 Ruby 및 Rails 스택에 추가하세요. Net::HTTP, HTTParty, Faraday, Mechanize와 함께 작동합니다.