# Re: Tricky socks question: changing source IP on Net::HTTP

**URL:** <https://www.ruby-forum.com/t/re-tricky-socks-question-changing-source-ip-on-net-http/108188>\
**Category:** Ruby\
**Created:** [June 29, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/re-tricky-socks-question-changing-source-ip-on-net-http/108188 "2007-06-29T21:31:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [June 29, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/re-tricky-socks-question-changing-source-ip-on-net-http/108188/1 "2007-06-29T21:31:13Z")

</div>

> I’ve got an interface with multiple IP addresses and I’m looking to  
> make HTTP requests from the non-primary IP.
> 
> How would I pull that off?

In principle, you ‘bind’ the socket to the correct local IP address  
before  
you ‘connect’

Unfortunately, TCPSocket#open (or #new) does the connect before it gives  
you  
a chance to bind. So you’ll need to replace TCPSocket.open with  
something  
messy along the lines of

```
s = Socket.new(Socket::PF_INET, Socket::SOCK_STREAM, 

```

Socket::IPPROTO\_TCP)  
s.bind(Socket.pack\_sockaddr\_in(0, “1.2.3.4”)) # chosen local IP  
s.connect(Socket.pack\_sockaddr\_in(80, “[pragprog.com](http://pragprog.com)”))

Test this and check that it works at the TCP layer, e.g. by adding

```
s.write("GET / HTTP/1.0\r\n\r\n")
puts s.read

```

If that works, then unfortunately net/http.rb doesn’t seem to have a  
suitable hook, so copy the source and look for TCPSocket.open to see  
where  
you need to modify it.

Good luck,

Brian.
