Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import websocket,hmac
- from websocket import create_connection
- from datetime import datetime
- import hashlib
- import websocket
- import json
- import struct
- import wave
- import pickle
- import pyaudio
- try:
- import thread
- except ImportError:
- import _thread as thread
- import time
- import sig_key
- def on_message(ws, message):
- print(message)
- def on_error(ws, error):
- print(error)
- def on_close(ws):
- print("### closed ###")
- def on_open(ws):
- 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, ())
- # HTTP verb
- method = "GET"
- # Service name
- service = "transcribe"
- # AWS Region
- region = "us-east-1"
- # Amazon Transcribe streaming endpoint
- endpoint = "https://transcribe-streaming.us-east-1.amazonaws.com:8443"
- # Host
- host = "transcribe-streaming.us-east-1.amazonaws.com:8443"
- # Date and time of request
- amz_date = datetime.now().strftime('%Y%m%dT%H%MZ')#'YYYMMDDTHHMMSSZ'
- # Date without time for credential scope
- print(amz_date)
- datestamp = datetime.now().strftime('%Y%m%d')
- canonical_uri = "/stream-transcription-websocket"
- canonical_headers = "host:" + host + "\n"
- signed_headers = "host"
- algorithm = "AWS4-HMAC-SHA256"
- access_key = sig_key.access_key
- credential_scope = datestamp + "/" + region + "/" + service + "/" + "aws4_request"
- canonical_querystring = "X-Amz-Algorithm=" + algorithm
- canonical_querystring += "&X-Amz-Credentials="+ access_key + "/" + credential_scope
- canonical_querystring += "&X-Amz-Date=" + amz_date
- canonical_querystring += "&X-Amz-Expires=300"
- # canonical_querystring += "&X-Amz-Security-Token=" + token
- canonical_querystring += "&X-Amz-SignedHeaders=" + signed_headers
- canonical_querystring += "&language-code=en-US&media-encoding=pcm&sample-rate=16000"
- payload_hash =hashlib.sha256("".encode('utf-8')).hexdigest()
- algorithm = 'AWS4-HMAC-SHA256'
- print(method)
- print(canonical_uri)
- print(canonical_querystring)
- print(signed_headers)
- print(payload_hash)
- canonical_request = (method + '\n' + canonical_uri + '\n'
- + canonical_querystring + '\n'
- + canonical_headers + '\n'
- + signed_headers + '\n'
- + payload_hash)
- string_to_sign = algorithm + '\n' + amz_date + '\n' + credential_scope + '\n' + hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
- # ************* TASK 3: CALCULATE THE SIGNATURE *************
- # Create the signing key using the function defined above.
- signing_key = sig_key.getSignatureKey(sig_key.secret_key, datestamp, region, service)
- # Sign the string_to_sign using the signing_key
- signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
- canonical_querystring += "&X-Amz-Signature=" + signature
- request_url = endpoint + canonical_uri + "?" + canonical_querystring
- print(request_url)
- if __name__ == "__main__":
- websocket.enableTrace(True)
- ws = websocket.WebSocketApp(request_url.replace('https','wss'),
- on_message = on_message,
- on_error = on_error,
- on_close = on_close)
- ws.on_open = on_open
- ws.run_forever()
Add Comment
Please, Sign In to add comment