The following causes all outgoing connections to be bound to a single non-default IP, including HTTP/HTTPS announces and peer connections. This IP must actually be assigned to the network device for this to work.
This does not affect the listening port.
Creds to OP for the HTTP announce part.
I'm still working on a way to make it pick the first item from config['bind'], but that seems a bit more difficult, as config[] isn't stored obviously in a nearby location.
BitTornado-0.3.17
Code:
diff -ur BitTornado-orig/SocketHandler.py BitTornado/SocketHandler.py
--- BitTornado-orig/SocketHandler.py 2007-09-05 03:19:32.000000000 -0500
+++ BitTornado/SocketHandler.py 2007-09-05 03:22:21.000000000 -0500
@@ -236,6 +236,7 @@
if handler is None:
handler = self.handler
sock = socket.socket(socktype, socket.SOCK_STREAM)
+ sock.bind(('172.16.44.11', 0)) # Put the originating IP for outgoing connections here
sock.setblocking(0)
try:
sock.connect_ex(dns)
diff -ur BitTornado-orig/zurllib.py BitTornado/zurllib.py
--- BitTornado-orig/zurllib.py 2007-09-05 03:19:43.000000000 -0500
+++ BitTornado/zurllib.py 2007-09-05 03:22:26.000000000 -0500
@@ -16,7 +16,28 @@
class btHTTPcon(HTTPConnection): # attempt to add automatic connection timeout
def connect(self):
- HTTPConnection.connect(self)
+ """Connect to the host and port specified in __init__."""
+ msg = "getaddrinfo returns an empty list"
+ for res in socket.getaddrinfo(self.host, self.port, 0,
+ socket.SOCK_STREAM):
+ af, socktype, proto, canonname, sa = res
+ try:
+ self.sock = socket.socket(af, socktype, proto)
+ self.sock.bind(('172.16.44.11', 0)) # Put the originating IP for outgoing connections here
+ if self.debuglevel > 0:
+ print "connect: (%s, %s)" % (self.host, self.port)
+ self.sock.connect(sa)
+ except socket.error, msg:
+ if self.debuglevel > 0:
+ print 'connect fail:', (self.host, self.port)
+ if self.sock:
+ self.sock.close()
+ self.sock = None
+ continue
+ break
+ if not self.sock:
+ raise socket.error, msg
+
try:
self.sock.settimeout(30)
except:
@@ -24,7 +45,14 @@
class btHTTPScon(HTTPSConnection): # attempt to add automatic connection timeout
def connect(self):
- HTTPSConnection.connect(self)
+ "Connect to a host on a given (SSL) port."
+
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ sock.bind(('172.16.44.11', 0)) # Put the originating IP for outgoing connections here
+ sock.connect((self.host, self.port))
+ ssl = socket.ssl(sock, self.key_file, self.cert_file)
+ self.sock = FakeSocket(sock, ssl)
+
try:
self.sock.settimeout(30)
except: