Wondering if there is a way to set a authorization header when making a client side socket connection with pythons socket module.
I have looked into this: Python requests library how to pass Authorization header with single token
And not sure if this is what I need to do as well.
I am trying to establish a websocket connection from a third party service and their docs provide an example with JS, but the python socket library I am using is a bit different. Zendesk Stream API
As the docs point out, after a connection is established, the Authorization header must be set with the OAuth Token.
var WebSocket = require('ws');
var ws_client = new WebSocket(
'wss://rtm.zopim.com/stream', {
headers: {
'Authorization': 'Bearer ' + {OAuth2 access token}
}
}
);
How can I set the Authorization
header with python socket library.
What I have so far:
import socket
class SockHandler():
def __init__(self):
#create a new socket
try:
self._s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Socket successfully created"
except socket.error as err:
print "socket creation failed with error %s" %(err)
def connect(self):
#connect to a remote socket address
HOST = self._s.gethostbyname("wss://rtm.zopim.com/stream")
self._s.connect((HOST, 443))
def sendMsg(self):
self._s.send("agents_online")
def receiveData(self):
data = self._s.recv(1024)
print("Data received: %s", data)
After self._s.connect((HOST, 443))
How can I set the Authorization
header with my OAuth Token?
EDIT: I need to also "subscribe" to event according to the Zendesk docs I provided above.
In the JS example, they send an object to subscribe to a socket such as
{
topic: "agents.{metric_key}",
action: "subscribe"
}
I am unsure how I can do this with python socket. The socket.send
only accepts a string and not an object, so I don't know how to send an object to subscribe.