support ipv6

This commit is contained in:
Matthias Urlichs 2013-12-12 07:00:58 +01:00
parent d66fd5ba1b
commit a7ac97eb82

View File

@ -176,12 +176,13 @@ class Reader(_FileLike):
class TCPClient: class TCPClient:
rbufsize = -1 rbufsize = -1
wbufsize = -1 wbufsize = -1
def __init__(self, host, port, source_address=None): def __init__(self, host, port, source_address=None, use_ipv6=False):
self.host, self.port = host, port self.host, self.port = host, port
self.connection, self.rfile, self.wfile = None, None, None self.connection, self.rfile, self.wfile = None, None, None
self.cert = None self.cert = None
self.ssl_established = False self.ssl_established = False
self.source_address = source_address self.source_address = source_address
self.use_ipv6 = use_ipv6
def convert_to_ssl(self, cert=None, sni=None, method=TLSv1_METHOD, options=None): def convert_to_ssl(self, cert=None, sni=None, method=TLSv1_METHOD, options=None):
""" """
@ -211,11 +212,10 @@ class TCPClient:
def connect(self): def connect(self):
try: try:
addr = socket.gethostbyname(self.host) connection = socket.socket(socket.AF_INET6 if self.use_ipv6 else socket.AF_INET, socket.SOCK_STREAM)
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.source_address: if self.source_address:
connection.bind(self.source_address) connection.bind(self.source_address)
connection.connect((addr, self.port)) connection.connect((self.host, self.port))
self.rfile = Reader(connection.makefile('rb', self.rbufsize)) self.rfile = Reader(connection.makefile('rb', self.rbufsize))
self.wfile = Writer(connection.makefile('wb', self.wbufsize)) self.wfile = Writer(connection.makefile('wb', self.wbufsize))
except (socket.error, IOError), err: except (socket.error, IOError), err: