1

I am trying to run a ruby script from my computer and I would like to have the script use a proxy IP address / server that I have setup, as opposed to the default IP address associated with my local machine.

I have been able to get my web browsers to use this proxy IP address by making changes inside network settings. But When I run the ruby script from textmate, it doesn't seem to use the proxy IP address I have put into my network settings. Instead it defaults back to the base ip address of my local machine.

Is there anything I can do in textmate or in the script itself to specify a proxy IP address it should route through?

My script looks like the following:

require "open-uri"
url = "some-url"
pattern = "<img"   


page = open(url).read
tags = page.scan(pattern)
puts "The site #{url} has #{tags.length} img tags"

Thanks for your help!

4

2 に答える 2

4

オプションを使用:proxyして、open-uriにプロキシサーバーを知らせます。

page = open(url, :proxy => "http://#{proxy_host}:#{proxy_port}/").read

代わりに環境変数を設定することもできますhttp_proxy。その場合は、:proxy => trueオプションを指定してください。

page = open(url, :proxy => true).read

[追加した]

基本認証でプロキシを使用する場合は、次:proxy_http_basic_authenticationの代わりにオプションを指定できます。:proxy

:proxy_http_basic_authentication => ["http://#{proxy_host}:#{proxy_port}/", login, password]

ruby1.9.2以降:proxy_http_basic_authenticationで使用できることに注意してください。

于 2012-12-10T02:15:38.477 に答える
0

正規表現の代わりにmechanizeとcssを使用することをお勧めします。

require "mechanize"
url = "http://www.google.com/"

@agent = Mechanize.new{|a| a.set_proxy 'localhost', 8888}
page = @agent.get url
tags = page.search('img')
puts "The site #{url} has #{tags.length} img tags"
于 2012-12-10T01:59:53.557 に答える