有没有办法将 Ruby Net::HTTP 请求附加到特定的 IP 地址/网络接口?

发布于 2024-09-05 05:04:36 字数 856 浏览 6 评论 0原文

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

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

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

有没有办法使用 Net::HTTP 库来做到这一点?作为替代方案 - CURB(红宝石卷曲绑定)。但这将是我尝试的最后一件事。

建议/想法?

PS CURB 的解决方案(脏测试,ip 被替换):

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

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.

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);

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.

Suggestions / Ideas?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只为一人 2024-09-12 05:04:36

我知道这已经很旧了,但希望其他人发现它有用,因为我今天需要它。您可以执行以下操作:

http = Net::HTTP.new(uri.host, uri.port)
http.local_host = ip
response = http.request(request)

请注意,我不相信您可以使用 Net::HTTP.start,因为它不接受 local_host 作为选项。

I know this is old, but hopefully someone else finds this useful, as I needed this today. You can do the following:

http = Net::HTTP.new(uri.host, uri.port)
http.local_host = ip
response = http.request(request)

Note that you I don't believe you can use Net::HTTP.start, as it doesn't accept local_host as an option.

相思碎 2024-09-12 05:04:36

事实上,如果你猴子修补 TCPSocket,有一种方法可以做到这一点:

https://gist.github.com/800214

Curb 很棒,但不能与 Jruby 一起使用,所以我一直在寻找替代方案......

There is in fact a way to do this if you monkey patch TCPSocket:

https://gist.github.com/800214

Curb is awesome but won't work with Jruby so I've been looking into alternatives...

顾铮苏瑾 2024-09-12 05:04:36

看起来你不能用 Net:HTTP 来做到这一点。这是源

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

第 644 行是打开连接的地方

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

TCPSocket.open 的第三个和第四个参数是 local_address 和 local_port,由于未指定它们,所以这是不可能的。看来你得走路边了。

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

Line 644 is where the connection is opened

  s = timeout(@open_timeout) { TCPSocket.open(conn_address(), conn_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.

北城半夏 2024-09-12 05:04:36

当然可以。我做了如下:

# remote_host can be IP or hostname
uri     = URI.parse( "http://" + remote_host )
http    = Net::HTTP.new( uri.host, uri.port )
request = Net::HTTP::Get.new(uri.request_uri)
request.initialize_http_header( { "Host" => domain })
response = http.request( request )

Of course you can. I did as below:

# remote_host can be IP or hostname
uri     = URI.parse( "http://" + remote_host )
http    = Net::HTTP.new( uri.host, uri.port )
request = Net::HTTP::Get.new(uri.request_uri)
request.initialize_http_header( { "Host" => domain })
response = http.request( request )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文