Skip to content

Instantly share code, notes, and snippets.

@agramajo
Created April 18, 2016 13:57
Show Gist options
  • Save agramajo/6145047aa49af419c8d2600797cfe752 to your computer and use it in GitHub Desktop.
Save agramajo/6145047aa49af419c8d2600797cfe752 to your computer and use it in GitHub Desktop.
set source ip address
import socket
import requests
real_create_conn = socket.create_connection
#diferentes ips que salen por diferentes enlaces
ip1 = '192.168.1.2'
ip2 = '192.168.1.3'
#forzamos la ip1
def set_src_addr1(*args):
address, timeout = args[0], args[1]
source_address = (ip1, 0)
return real_create_conn(address, timeout, source_address)
#forzamos la ip2
def set_src_addr2(*args):
address, timeout = args[0], args[1]
source_address = (ip2, 0)
return real_create_conn(address, timeout, source_address)
socket.create_connection = set_src_addr1
print ip1, requests.get('http://baicom.com/ip').content
socket.create_connection = set_src_addr2
print ip2, requests.get('http://baicom.com/ip').content
@hossinasaadi
Copy link

it's not working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment