My websocket client is trying to talk to a remote wss server, and is failing with this output:
[my-user@my-server]$ python my_websocket_client.py
ws-client connecting...
[Errno 111] Connection refused
conn closed
exception in main: 'NoneType' object has no attribute 'status'
ws-client connect status is not ok.
trying to reconnect
ws-client connecting...
[Errno 111] Connection refused
conn closed
exception in main 'NoneType' object has no attribute 'status'
...and it just repeats that over and over.
Here's the relevant code (client side):
def on_error(ws, error):
logger.error("on error is: %s" % error)
def reconnect():
global reconnect_count
logger.warning("ws-client connect status is not ok.\ntrying to reconnect for the %d time" % reconnect_count)
reconnect_count += 1
if reconnect_count < RECONNECT_MAX_TIMES:
thread.start_new_thread(connect, ())
def on_message(ws, message):
message_json = json.loads(message)
payload = base64_decode_as_string(message_json["payload"])
# handler payload
try:
message_handler(payload)
except Exception as e:
logger.error("handler message, a business exception has occurred,e:%s" % e)
send_ack(message_json["messageId"])
def on_close(obj):
logging.critical("Connection closed!")
obj.close()
global connect_status
connect_status = 0
def connect():
logger.info("ws-client connecting...")
ws.run_forever(sslopt=SSL_OPT, ping_interval=PING_INTERVAL_SECONDS, ping_timeout=PING_TIMEOUT_SECONDS)
def send_ack(message_id):
json_str = json.dumps({"messageId": message_id})
ws.send(json_str)
def main():
header = {"Connection": "Upgrade",
"username": ACCESS_ID,
"password": gen_pwd()}
websocket.setdefaulttimeout(CONNECT_TIMEOUT_SECONDS)
global ws
ws = websocket.WebSocketApp(get_topic_url(),
header=header,
on_message=on_message,
on_error=on_error,
on_close=on_close)
thread.start_new_thread(connect, ())
while True:
time.sleep(CHECK_INTERVAL_SECONDS)
global reconnect_count
global connect_status
try:
if ws.sock.status == 101:
# annoying
# print("ws-client connect status is ok.")
reconnect_count = 1
connect_status = 1
except Exception:
connect_status = 0
reconnect()
if __name__ == '__main__':
main()
Also, ws.sock is None.
The reason, I think, is because the server is trying to make a connection back to the client to a high port number; however, only a few ports like 80, 443 are available to reach back to the client.
I see in my code it uses run_forever. The documentation says this function has arguments for proxies, but the documentation doesn't give an overview of that process, isn't clear how to make that happen, and doesn't show what that looks like conceptually.
How can I make the server send messages to a proxy on port 443, which in turn talks to my websocket client, to help it overcome the unavailability of other port numbers?
Or, even better, how can I make the client tell the server to connect back to it only on port 443?
Note: I'm asking the question because there are conceptual things I don't understand and aren't clear in any of the available documentation. If it was, I wouldn't be asking.