TypeError:需要类似字节的对象,而不是';str';-python 2到3
嗨,我遇到了这个错误消息的问题。我是Python新手,这个Python 2和Python 3很麻烦。我不确定该怎么办,错误消息如下所示TypeError:需要类似字节的对象,而不是';str';-python 2到3,python,typeerror,python-2to3,Python,Typeerror,Python 2to3,嗨,我遇到了这个错误消息的问题。我是Python新手,这个Python 2和Python 3很麻烦。我不确定该怎么办,错误消息如下所示 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: " +
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()
使用Ticker:AAPL
回溯(最近一次呼叫最后一次):
文件“realtime.py”,第18行,在
r=请求.get(auth_url,headers={“授权”:“基本%s”%base64.b64encode(os.environ['INTRINIO_USER']+:“+os.environ['INTRINIO_PASSWORD']))
文件“C:\Users\User\AppData\Local\Programs\Python36-32\lib\base64.py”,第58行,b64encode
encoded=binascii.b2a_base64(s,换行符=False)
TypeError:需要类似字节的对象,而不是“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()
您应该将
str
编码为字节
data_string = os.environ['INTRINIO_USER'] + ":" + os.environ['INTRINIO_PASSWORD']
data_bytes = data_string.encode("utf-8")
base64.b64encode(data_bytes)
相关信息:我不知道应该把它放在哪里,尽管你正在传递一个字符串
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_密码”]这是打字错误吗?在哪里使用了“data_str”和“data_bytes”?行:r=requests.get(auth_url,headers={“Authorization”:“Basic%s”%base64.b64encode(os.environ['INTRINIO_USER']+”:“+os.environ['INTRINIO_密码']))使用r=requests.get(auth_url,headers={“Authorization”:“Basic%s”%base64.b64encode['INTRINIO_PASSWORD'])。改为编码(“utf-8”)};