From 9c7e93e9247d9aaca353e3c16e09293b9d124e0a Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Thu, 21 Mar 2019 13:36:09 -0400 Subject: [PATCH] detect connection failures --- ws_client.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ws_client.py b/ws_client.py index 0ec4badaf..25bf4d2fb 100644 --- a/ws_client.py +++ b/ws_client.py @@ -3,8 +3,10 @@ from __future__ import print_function import sys import json +from twisted.internet.error import ConnectError from twisted.internet.task import react from twisted.internet.defer import inlineCallbacks, Deferred +from twisted.internet.endpoints import HostnameEndpoint from autobahn.twisted.websocket import ( WebSocketClientProtocol, @@ -65,10 +67,14 @@ def main(reactor): factory.on_close = Deferred() factory.protocol = TahoeLogProtocol - port = yield reactor.connectTCP("127.0.0.1", int(port), factory) - # okay, I give up: how do we detect that our connection was - # refused? + endpoint = HostnameEndpoint(reactor, "127.0.0.1", int(port)) + try: + port = yield endpoint.connect(factory) + except ConnectError as e: + print("Connection failed: {}".format(e)) + return + print("port: {}".format(port)) yield factory.on_open print("opened")