python - UnicodeDecodeError : 'utf-8' codec can't decode byte 0xe2 in position 1023: unexpected end of data

标签 python python-3.x irc twitch

基本上我已经用 Python 制作了一个 IRC Twitch 机器人,它现在除了加入 channel 之外什么都不做。乒乓循环可以正常工作一段时间,但随后因标题中的错误而停止。我做错了什么?提前致谢。

import re
import socket

HOST = "irc.twitch.tv"
PORT = 6667
NICK = "asdsad"
PASS = "oauth:asdasdasdasd"
channel = "#coolperson"

def send_message(sock, msg):
    sock.send("PRIVMSG #{} {}".format(channel, msg))

s = socket.socket()
s.connect((HOST, PORT))
s.send("PASS {}\r\n".format(PASS).encode("utf-8"))
s.send("NICK {}\r\n".format(NICK).encode("utf-8"))
s.send("JOIN {}\r\n".format(channel).encode("utf-8"))

while True:
    response = s.recv(1024).decode("utf-8")
    if response == "PING :tmi.twitch.tv\r\n":
        s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
        print("answered the call")

最佳答案

您可以跳过该错误。
而不是以下部分:

response = s.recv(1024).decode("utf-8")

使用这个:
response = s.recv(1024).decode('utf-8', 'ignore')

关于python - UnicodeDecodeError : 'utf-8' codec can't decode byte 0xe2 in position 1023: unexpected end of data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53062552/

相关文章:

python - 缺少值的 DataFrame 列将不接受输入

python - Django 自引用关系?

chat - 互联网聊天服务,如 IRC,但有消息历史记录

c++ - 如何读取IRC数据包?

node.js - 如何通过 IRC 适配器使用 Hubot 和 node-cron

python - 使用 pygame 在 python 中通过套接字进行流式传输

python - Django datetime.timedelta ,如果它们可能是不同的集合,它如何从 timezone.now() 中减去

Python:如何填写网络表单并获取结果页面源代码

python-3.x - 不和谐.py |检查是否有用户加入语音 channel

python - 模块未找到错误: No module named 'tensorflow.compiler'