Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding a particular IP #394

Closed
jibaku opened this issue Jan 27, 2012 · 15 comments
Closed

Binding a particular IP #394

jibaku opened this issue Jan 27, 2012 · 15 comments

Comments

@jibaku
Copy link

jibaku commented Jan 27, 2012

It would be great to be able to bound a particular IP as the source address (for hosts with multiple IP addresses).

@kennethreitz
Copy link
Contributor

Interesting. How do you invision the usage?

@kennethreitz
Copy link
Contributor

^ urllib3/urllib3#9

@jibaku
Copy link
Author

jibaku commented Jan 27, 2012

The goal is to write a crawler using an IP associated with a particular country. But I don't know how it would be used on a API level.

@kennethreitz
Copy link
Contributor

Couldn't you just send a request to an IP with a particular Host header?

@jibaku
Copy link
Author

jibaku commented Jan 29, 2012

No, because it's to have the specific IP in the web server log (to be reversed to a particular country using tools like GeoIP).

@kennethreitz
Copy link
Contributor

I don't follow.

>>> requests.get('http://184.106.67.239', headers={'host': 'ci.kennethreitz.com'})
<Response [200]>

@piotr-dobrogost
Copy link

import socket
import urllib2
true_socket = socket.socket

def make_bound_socket(source_ip):
    def bound_socket(*a, **k):
        sock = true_socket(*a, **k)
        sock.bind((source_ip, 0))
        return sock
    return bound_socket

socket.socket = make_bound_socket('<some source ip>')
print urllib2.urlopen('http://httpbin.org/ip').read()

socket.socket = make_bound_socket('<some other source ip>')
print urllib2.urlopen('http://httpbin.org/ip').read()

If traffic going out from the two source ip numbers goes out through two different public ip numbers one should see these different public numbers in the origin field of the response.

@kennethreitz
Copy link
Contributor

I think this is out of scope.

@emidln
Copy link

emidln commented Nov 12, 2012

For people interested in this still, you might want to take a look at urlib3's HttpConnectionPool, in particular where new httplib/http.client HttpConnections are spun up so you can pass in source_address. If you do that, then you can keep on exposing this in request's Request model, and use it as you wish.

@jessesherlock
Copy link

For those arriving here from google, you will want to follow this: urllib3/urllib3#9 (comment)

@petri
Copy link

petri commented May 14, 2014

This should be reopened - it's not out of scope, and it's quite necessary when working with hosts inside a VPN.

Depending on specifics of the address spaces of the current & remote network, one often has to send requests from the IP bound to the interface set up by the VPN. Or make changes to the routing table. This applies at least to using NetExtender on a Mac.

Many networking programs such as ssh and curl support setting the interface to use / binding to IP of an interface.

@Lukasa
Copy link
Member

Lukasa commented May 14, 2014

@petri Your assertion that this is not out of scope is incorrect, given that the scope of the Requests project is defined entirely by Kenneth. =)

Specifically, we view it as being out of scope for the core requests project. However, you can still do it, and it's not even very hard. This discussion covers how to do it.

@petri
Copy link

petri commented May 14, 2014

Nice - I was not aware of that kind of mechanism in requests. Thanks!

@danlesko
Copy link

import socket
import urllib2
true_socket = socket.socket

def make_bound_socket(source_ip):
    def bound_socket(*a, **k):
        sock = true_socket(*a, **k)
        sock.bind((source_ip, 0))
        return sock
    return bound_socket

socket.socket = make_bound_socket('<some source ip>')
print urllib2.urlopen('http://httpbin.org/ip').read()

socket.socket = make_bound_socket('<some other source ip>')
print urllib2.urlopen('http://httpbin.org/ip').read()

If traffic going out from the two source ip numbers goes out through two different public ip numbers one should see these different public numbers in the origin field of the response.

Just wanted you to know I've been scouring the internet for forever and this was the only solution that worked for me.

@sigmavirus24
Copy link
Contributor

@psf psf locked as resolved and limited conversation to collaborators Dec 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants