ayushkhare12

Untitled

Oct 7th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. import websocket,hmac
  2. from websocket import create_connection
  3. from datetime import datetime
  4. import hashlib
  5. import websocket
  6. import json
  7. import struct
  8. import wave
  9. import pickle
  10. import pyaudio
  11.  
  12. try:
  13.     import thread
  14. except ImportError:
  15.     import _thread as thread
  16. import time
  17. import sig_key
  18.  
  19. def on_message(ws, message):
  20.     print(message)
  21.  
  22. def on_error(ws, error):
  23.     print(error)
  24.  
  25. def on_close(ws):
  26.     print("### closed ###")
  27.  
  28. def on_open(ws):
  29.     def run(*args):
  30.         for i in range(3):
  31.             time.sleep(1)
  32.             ws.send("Hello %d" % i)
  33.         time.sleep(1)
  34.         ws.close()
  35.         print("thread terminating...")
  36.     thread.start_new_thread(run, ())
  37.  
  38.  
  39. # HTTP verb
  40. method = "GET"
  41. # Service name
  42. service = "transcribe"
  43. # AWS Region
  44. region = "us-east-1"
  45. # Amazon Transcribe streaming endpoint
  46. endpoint = "https://transcribe-streaming.us-east-1.amazonaws.com:8443"
  47. # Host
  48. host = "transcribe-streaming.us-east-1.amazonaws.com:8443"
  49. # Date and time of request
  50. amz_date = datetime.now().strftime('%Y%m%dT%H%MZ')#'YYYMMDDTHHMMSSZ'
  51. # Date without time for credential scope
  52. print(amz_date)
  53. datestamp = datetime.now().strftime('%Y%m%d')
  54. canonical_uri = "/stream-transcription-websocket"
  55. canonical_headers = "host:" + host + "\n"
  56. signed_headers = "host"
  57. algorithm = "AWS4-HMAC-SHA256"
  58. access_key = sig_key.access_key
  59. credential_scope = datestamp + "/" + region + "/" + service + "/" + "aws4_request"
  60. canonical_querystring  = "X-Amz-Algorithm=" + algorithm
  61. canonical_querystring += "&X-Amz-Credentials="+ access_key + "/" + credential_scope
  62. canonical_querystring += "&X-Amz-Date=" + amz_date
  63. canonical_querystring += "&X-Amz-Expires=300"
  64. # canonical_querystring += "&X-Amz-Security-Token=" + token
  65. canonical_querystring += "&X-Amz-SignedHeaders=" + signed_headers
  66. canonical_querystring += "&language-code=en-US&media-encoding=pcm&sample-rate=16000"
  67. payload_hash =hashlib.sha256("".encode('utf-8')).hexdigest()
  68. algorithm = 'AWS4-HMAC-SHA256'
  69. print(method)
  70. print(canonical_uri)
  71. print(canonical_querystring)
  72. print(signed_headers)
  73. print(payload_hash)
  74. canonical_request = (method + '\n' + canonical_uri + '\n'
  75. + canonical_querystring + '\n'
  76. + canonical_headers + '\n'
  77. + signed_headers + '\n'
  78. + payload_hash)
  79.  
  80. string_to_sign = algorithm + '\n' +  amz_date + '\n' +  credential_scope + '\n' +  hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
  81.  
  82. # ************* TASK 3: CALCULATE THE SIGNATURE *************
  83. # Create the signing key using the function defined above.
  84. signing_key = sig_key.getSignatureKey(sig_key.secret_key, datestamp, region, service)
  85.  
  86. # Sign the string_to_sign using the signing_key
  87. signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest()
  88.  
  89. canonical_querystring += "&X-Amz-Signature=" + signature
  90. request_url = endpoint + canonical_uri + "?" + canonical_querystring
  91. print(request_url)
  92.  
  93.  
  94.  
  95. if __name__ == "__main__":
  96.     websocket.enableTrace(True)
  97.     ws = websocket.WebSocketApp(request_url.replace('https','wss'),
  98.  
  99.                               on_message = on_message,
  100.                               on_error = on_error,
  101.                               on_close = on_close)
  102.     ws.on_open = on_open
  103.     ws.run_forever()
Add Comment
Please, Sign In to add comment