TypeError:需要一个类似字节的对象,而不是'str'-python 2到3 [重复]
Posted
技术标签:
【中文标题】TypeError:需要一个类似字节的对象,而不是\'str\'-python 2到3 [重复]【英文标题】:TypeError: a bytes-like object is required, not 'str' - python 2 to 3 [duplicate]TypeError:需要一个类似字节的对象,而不是'str'-python 2到3 [重复] 【发布时间】:2018-01-10 22:39:55 【问题描述】:您好,我在处理此错误消息时遇到了问题。我是 Python 新手,这个 Python2 和 Python3 很麻烦。我不知道这里该怎么做,错误信息如下所示。
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'
我使用的代码如下所示。
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()
【问题讨论】:
相关:***.com/questions/21689365/… 您正在将字符串os.environ['INTRINIO_USER'] + ":" + os.environ['INTRINIO_PASSWORD']
传递给base64.b64encode()
,对吧?
是的所以 r=requests.get(auth_url, headers="Authorization": "Basic %s" % base64.b64encode(os.environ['INTRINIO_USER'] + ":" + os. environ['INTRINIO_PASSWORD'], 'uft-8'))?
【参考方案1】:
您应该将str
编码为bytes
。
data_string = os.environ['INTRINIO_USER'] + ":" + os.environ['INTRINIO_PASSWORD']
data_bytes = data_string.encode("utf-8")
base64.b64encode(data_bytes)
【讨论】:
这是错字吗? “data_str”和“data_bytes”在哪里使用? 行:r=requests.get(auth_url, headers="Authorization": "Basic %s" % base64.b64encode(os.environ['INTRINIO_USER'] + ":" + os. environ['INTRINIO_PASSWORD'])) 使用 r=requests.get(auth_url, headers="Authorization": "Basic %s" % base64.b64encode((os.environ['INTRINIO_USER'] + ":" + os .environ['INTRINIO_PASSWORD']).encode("utf-8"))) 代替;以上是关于TypeError:需要一个类似字节的对象,而不是'str'-python 2到3 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
“TypeError:在 OAuth 2.0 回调请求期间需要一个类似字节的对象,而不是 'str'”
诱变剂:TypeError:需要一个类似字节的对象,而不是“str”
TypeError:需要一个类似字节的对象,而不是“str”套接字编程
Django Rest Framework TypeError需要一个类似字节的对象,而不是'str'