Feature #6617


Net::HTTP: Bind to a specific local IP/port

Added by mksm (Ricardo Amorim) about 10 years ago. Updated almost 10 years ago.

Target version:


Despite having several solutions floating around the net, this feature is currently not present in Net::HTTP. I searched the issues but didn't find any discussion on this. How does the dev team feel about this? Can I submit a patch for revision?


net-http_bind_to_local_ip.patch (1.25 KB) net-http_bind_to_local_ip.patch mksm (Ricardo Amorim), 06/26/2012 03:44 AM
net-http_bind_to_local_ip-tests.patch (1.46 KB) net-http_bind_to_local_ip-tests.patch mksm (Ricardo Amorim), 07/05/2012 02:19 PM

Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #6746: test_bind_to_local_host(TestNetHTTPLocalBind) fails.Closedshyouhei (Shyouhei Urabe)07/17/2012Actions

Updated by drbrain (Eric Hodel) about 10 years ago

  • Category set to lib

Bind to a local port like does? I'm sure this feature could be added.

I think the easiest way to add it would result in an API like:

http = remote_host
http.local_host = local_host
http.start do


Instead of passing the local host and port as parameters to or Net::HTTP.start.

Updated by mksm (Ricardo Amorim) about 10 years ago

I agree. You can pass both local_ip and local_port to in Net::HTTP#connect.

Having the setter exposed would allow the user to change the local IP address between connections. Any ideas if this could cause problems with Net::HTTP or remote servers? There's also proxy and ipv6 support, both which I have never tested using this approach.

Updated by mksm (Ricardo Amorim) about 10 years ago

The patch adds accessors for @local_host and @local_port and uses both in call.

Updated by drbrain (Eric Hodel) about 10 years ago

  • Assignee set to naruse (Yui NARUSE)
  • Target version set to 2.0.0

Updated by naruse (Yui NARUSE) almost 10 years ago

It looks good.
Could you provide a test for it?
It should be a patch for test/net/http/test_http.rb

Updated by madeofcode (Mark Dodwell) almost 10 years ago

Forgive me if this is a silly question, but what is the point of initializing the instance variables to nil in the initializer?

Updated by jballanc (Joshua Ballanco) almost 10 years ago

Not sure, but it may be related to this:

>> defined?(@foo)
=> nil 
>> @foo = nil
=> nil 
>> defined?(@foo)
=> "instance-variable" 


Updated by mksm (Ricardo Amorim) almost 10 years ago

@naruse (Yui NARUSE): tests attached! It seems that local_host cannot be nil when setting local_port in

@madeofcode (Mark Dodwell): I also do not know, just followed the pattern. jballanc may have a point, but i can't see why there would be #defined? checks on ivars.

Updated by drbrain (Eric Hodel) almost 10 years ago

You need to initialize instance variables to prevent warnings on uninitialized access:

$ ruby -we 'p @x'
-e:1: warning: instance variable @x not initialized

Actions #10

Updated by naruse (Yui NARUSE) almost 10 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r36367.
Ricardo, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

  • lib/net/http.rb (Net::HTTP#connect): use local_host and local_port
    if specified. patched by Ricardo Amorim [Feature #6617]

Also available in: Atom PDF