- I don't have IPv6 enviroment, now. So, I didn't test yet.
- fixed some test warning.
This commit is contained in:
liris
2014-04-30 14:19:33 +09:00
parent 8fbb15a6ec
commit 3f560ca541
2 changed files with 20 additions and 10 deletions

View File

@@ -459,10 +459,9 @@ class SockOptTest(unittest.TestCase):
@unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled") @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
def testSockOpt(self): def testSockOpt(self):
sockopt = ((socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),) sockopt = ((socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),)
s = ws.WebSocket(sockopt=sockopt)
self.assertNotEqual(s.sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY), 0)
s = ws.create_connection("ws://echo.websocket.org", sockopt=sockopt) s = ws.create_connection("ws://echo.websocket.org", sockopt=sockopt)
self.assertNotEqual(s.sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY), 0) self.assertNotEqual(s.sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY), 0)
s.close()
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -418,11 +418,9 @@ class WebSocket(object):
if sslopt is None: if sslopt is None:
sslopt = {} sslopt = {}
self.connected = False self.connected = False
self.sock = socket.socket() self.sock = None
for opts in DEFAULT_SOCKET_OPTION: self._timeout = None
self.sock.setsockopt(*opts) self.sockopt = sockopt
for opts in sockopt:
self.sock.setsockopt(*opts)
self.sslopt = sslopt self.sslopt = sslopt
self.get_mask_key = get_mask_key self.get_mask_key = get_mask_key
self.fire_cont_frame = fire_cont_frame self.fire_cont_frame = fire_cont_frame
@@ -454,7 +452,7 @@ class WebSocket(object):
""" """
Get the websocket timeout(second). Get the websocket timeout(second).
""" """
return self.sock.gettimeout() return self._timeout
def settimeout(self, timeout): def settimeout(self, timeout):
""" """
@@ -462,7 +460,7 @@ class WebSocket(object):
timeout: timeout time(second). timeout: timeout time(second).
""" """
self.sock.settimeout(timeout) self._timeout = timeout
timeout = property(gettimeout, settimeout) timeout = property(gettimeout, settimeout)
@@ -487,8 +485,21 @@ class WebSocket(object):
""" """
hostname, port, resource, is_secure = _parse_url(url) hostname, port, resource, is_secure = _parse_url(url)
addrinfo_list = socket.getaddrinfo(hostname, 80, 0, 0, socket.SOL_TCP)
if not addrinfo_list:
raise WebSocketException("Host not found.: " + hostname + ":" + str(port))
family = addrinfo_list[0][0]
self.sock = socket.socket(family)
self.sock.settimeout(self.timeout)
for opts in DEFAULT_SOCKET_OPTION:
self.sock.setsockopt(*opts)
for opts in self.sockopt:
self.sock.setsockopt(*opts)
# TODO: we need to support proxy # TODO: we need to support proxy
self.sock.connect((hostname, port)) address = addrinfo_list[0][4]
self.sock.connect(address)
if is_secure: if is_secure:
if HAVE_SSL: if HAVE_SSL:
sslopt = dict(cert_reqs=ssl.CERT_REQUIRED, sslopt = dict(cert_reqs=ssl.CERT_REQUIRED,