From 02376b6a75fdb397a865697723f7282dbf70deca Mon Sep 17 00:00:00 2001 From: Andrey Plotnikov Date: Sun, 7 Jul 2013 13:33:56 +0800 Subject: [PATCH] Add socket binding support for TCPClient --- netlib/tcp.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/netlib/tcp.py b/netlib/tcp.py index 479537242..b5e9e2c45 100644 --- a/netlib/tcp.py +++ b/netlib/tcp.py @@ -173,11 +173,12 @@ class Reader(_FileLike): class TCPClient: rbufsize = -1 wbufsize = -1 - def __init__(self, host, port): + def __init__(self, host, port, source_address=None): self.host, self.port = host, port self.connection, self.rfile, self.wfile = None, None, None self.cert = None self.ssl_established = False + self.source_address = source_address def convert_to_ssl(self, cert=None, sni=None, method=TLSv1_METHOD, options=None): """ @@ -209,6 +210,8 @@ class TCPClient: try: addr = socket.gethostbyname(self.host) connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + if self.source_address: + connection.bind(self.source_address) connection.connect((addr, self.port)) self.rfile = Reader(connection.makefile('rb', self.rbufsize)) self.wfile = Writer(connection.makefile('wb', self.wbufsize))