Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: a bytes-like object is required, not 'str' - python 2 to 3 [duplicate]

Hi I am having trouble with this error message. I am new to Python and this Python2 and Python3 is a hassle. I'm not sure what to do here, the error message is as shown below.

Using Ticker: AAPL     Traceback (most recent call last):       File "realtime.py", line 18, in <module>         r=requests.get(auth_url, headers={"Authorization": "Basic %s" % base64.b64encode(os.environ['INTRINIO_USER'] + ":" + os.environ['INTRINIO_PASSWORD'])})       File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\base64.py", line 58, in b64encode         encoded = binascii.b2a_base64(s, newline=False)     TypeError: a bytes-like object is required, not 'str' 

The code I am using is as shown below.

import websocket import _thread import time import requests import base64 import json import sys import os from requests.auth import HTTPBasicAuth  try:     print ("Using Ticker: " + str(sys.argv[1])) except:     print ("Please include ticker as first argument")     sys.exit()  auth_url = "https://realtime.intrinio.com/auth"; r=requests.get(auth_url, headers={"Authorization": "Basic %s" % base64.b64encode(os.environ['INTRINIO_USER'] + ":" + os.environ['INTRINIO_PASSWORD'])})  socket_target = "wss://realtime.intrinio.com/socket/websocket?token=%s" % (r.text)  def on_message(ws, message):     try:         result = json.loads(message)         print (result["payload"])     except:         print (message)  def on_error(ws, error):     print ("###ERROR### " + error)  def on_close(ws):     print ("###CONNECTION CLOSED###")  def on_open(ws):     def run(*args):         security = "iex:securities:" + str(sys.argv[1]).upper()         message = json.dumps({"topic": security,"event": "phx_join","payload": {},"ref": "1"})         ws.send(message)     thread.start_new_thread(run, ())   websocket.enableTrace(True) ws = websocket.WebSocketApp(socket_target, on_message = on_message, on_error = on_error, on_close = on_close) ws.on_open = on_open ws.run_forever() 
like image 944
Lasheen Lartey Avatar asked Aug 03 '17 10:08

Lasheen Lartey


People also ask

How do you fix a bytes like an object is required not str?

You cannot write a string to a file, you need to encode the string to a byte-like object to do so. By running the encode() method of a string, we get the encoded version of it in the default encoding, which is usually utf-8 .

How do I fix Typeerror a bytes like object is required not str in Python?

Binary files are considered a series of bytes data and not as a string. It means that all data read from the file is returned as bytes objects, not str. We can solve this error by opening the file in read-only mode instead of binary mode, as shown below.

What is a byte like object in Python?

Bytes-like object in python In Python, a string object is a series of characters that make a string. In the same manner, a byte object is a sequence of bits/bytes that represent data. Strings are human-readable while bytes are computer-readable. Data is converted into byte form before it is stored on a computer.


1 Answers

You should encode str into bytes.

data_string = os.environ['INTRINIO_USER'] + ":" + os.environ['INTRINIO_PASSWORD']  data_bytes = data_string.encode("utf-8")  base64.b64encode(data_bytes) 
like image 191
stamaimer Avatar answered Sep 30 '22 04:09

stamaimer