有没有一种方法可以将Ruby Net :: HTTP请求附加到特定的IP地址/网络接口? [英] Is there a way to attach Ruby Net::HTTP request to a specific IP address / network interface?

查看:71
本文介绍了有没有一种方法可以将Ruby Net :: HTTP请求附加到特定的IP地址/网络接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用标准Net :: HTTP库为每个GET请求使用不同IP地址的方法.服务器有5个IP地址,并且假设达到每个IP的请求限制时,某些API会阻止访问.因此,唯一的方法是-使用另一台服务器.我在ruby文档中找不到任何有关它的信息.

Im looking a way to use different IP addresses for each GET request with standard Net::HTTP library. Server has 5 ip addresses and assuming that some API`s are blocking access when request limit per IP is reached. So, only way to do it - use another server. I cant find anything about it in ruby docs.

例如,curl允许您将其附加到特定的IP地址(在PHP中):

For example, curl allows you to attach it to specific ip address (in PHP):

$req = curl_init($url)
curl_setopt($req, CURLOPT_INTERFACE, 'ip.address.goes.here';
$result = curl_exec($req);

使用Net :: HTTP库有什么办法吗?作为替代-CURB(红宝石卷曲装订).但这将是我将要尝试的最后一件事.

Is there any way to do it with Net::HTTP library? As alternative - CURB (ruby curl binding). But it will be the last thing i`ll try.

建议/想法?

P.S. CURB的解决方案(带有肮脏的测试,已替换ip):

P.S. The solution with CURB (with dirty tests, ip`s being replaced):

require 'rubygems'
require 'curb'

ip_addresses = [
  '1.1.1.1',
  '2.2.2.2',
  '3.3.3.3',
  '4.4.4.4',
  '5.5.5.5'
]

ip_addresses.each do |address|
  url = 'http://www.ip-adress.com/'
  c = Curl::Easy.new(url)
  c.interface = address
  c.perform
  ip = c.body_str.scan(/<h2>My IP address is: ([\d\.]{1,})<\/h2>/).first
  puts "for #{address} got response: #{ip}"
end

推荐答案

看起来无法使用Net:HTTP来完成.这是来源

Doesn't look like you can do it with Net:HTTP. Here's the source

http://github.com/ruby/ruby /blob/trunk/lib/net/http.rb

第644行是打开连接的地方

Line 644 is where the connection is opened

  s = timeout(@open_timeout) { TCPSocket.open(conn_address(), conn_port()) }

TCPSocket.open的第三个和第四个参数是local_address和local_port,由于未指定它们,因此是不可能的.看来您必须要遏制.

The third and fourth arguments to TCPSocket.open are local_address and local_port, and since they're not specified, it's not possible. Looks like you'll have to go with curb.

这篇关于有没有一种方法可以将Ruby Net :: HTTP请求附加到特定的IP地址/网络接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆