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!

2
  • Hmm, are you developing under mac os? If so, please add this tag :) Commented Dec 10, 2012 at 1:53
  • yes - on a mac, if that's what you mean - will add tag Commented Dec 10, 2012 at 1:57

2 Answers 2

4

Use :proxy option to let open-uri know your proxy server:

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

You can also set environment variable http_proxy instead. If you do so, give :proxy => true for option.

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

[ADDED]

If you want to use proxy with basic authentication, you can give :proxy_http_basic_authentication option instead of :proxy as follows:

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

Note that :proxy_http_basic_authentication can be used in ruby 1.9.2 or later.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool - this makes sense. Is there a clean easy way to pass in a username and password for the proxy server?
0

I recommend using mechanize, and css instead of regex:

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.