AnsweredAssumed Answered

Connecting to a channel w/ Python

Question asked by dpanseri on Mar 7, 2018
Latest reply on Mar 9, 2018 by rdavin

I'm attempting to connect to a channel via python using the websocket-client library (see: GitHub - websocket-client/websocket-client: websocket client for python ). I've got a basic script that attempts to connect to a channel. When I do I keep getting 400 Bad Request responses. My code is below, any assistance is greatly appreciated. Note I've edited out the web ID from my system. Assume they are correct.

 

import websocket
import ssl

try:
    import thread
except ImportError:
    import _thread as thread
import time

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    print("Open")
    def run(*args):
        for i in range(3):
            time.sleep(1)
            #ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
        print("thread terminating...")
    thread.start_new_thread(run, ())

if __name__ == "__main__":
    webid = "" #real web ID omitted assume it's valid
    options = ""
    uri = "wss://piserver1/piwebapi/streams/" + webid + "/channel" + options
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(uri,
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)

    ws.on_open = on_open
    ws.run_forever(sslopt={"cert_reqs": ssl.VERIFY_DEFAULT})

 

Output:

--- request header ---

GET /piwebapi/streams/*webid omitted*/channel HTTP/1.1

Upgrade: websocket

Connection: Upgrade

Host: piserver1

Origin: http://piserver1

Sec-WebSocket-Key: qNBHhTQqaWPOdKun45dlDg==

Sec-WebSocket-Version: 13

 

-----------------------

--- response header ---

HTTP/1.1 400 Bad Request

Transfer-Encoding: chunked

Content-Type: application/json; charset=utf-8

Server: Microsoft-HTTPAPI/2.0

X-Frame-Options: SAMEORIGIN

Date: Wed, 07 Mar 2018 16:36:58 GMT

-----------------------

Handshake status 400 Bad Request

 

Message was edited by: Rick Davin to add Python Syntax Highlighting

Outcomes