The following code binds specified ip address to socket in main program globally.
import socket
true_socket = socket.socket
def bound_socket(*a, **k):
sock = true_socket(*a, **k)
sock.bind((sourceIP, 0))
return sock
socket.socket = bound_socket
Suppose main program has 10 threads, each with a urllib2 instance running inside the thread. How to bind 10 different ip addresses to each urllib2 object?