Python 错误:参数 1 必须是 buffer 或 bytes 而不是 str
该程序很简单,旨在连接到 IRC 房间。问题是,当我尝试将我的机器人连接到它时,它给我标题中的错误。我不确定他们想要什么而不是字符串。我不确定缓冲区或字节指的是什么。其他人已经让这个脚本工作了,但它对我不起作用。注意:这不是恶意的 irc 机器人或其他什么。这只是一些基本网络的练习。
import socket
network = 'irc.rizon.net'
port = 6667
irc = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
irc.connect((network,port))
irc.send("NICK PyBot\r\n")
irc.send("USER Pybot Pybot Pybot : Python IRC\r\n")
irc.send("JOIN #pychat\r\n")
irc.send("PART #pychat\r\n")
irc.send("QUITE\r\n")
irc.close()
The program is simple, it is meant to connect to an IRC room. The problem is that when I try to connect my bot to it, it gives me the error in the title. I am not sure what they want instead of a string. I am not sure what buffer or bytes refers to. Others have gotten this script to work, but its not working for me. Note: This is not a malicous irc bot or whatever. This is just an exercise with some basic networking.
import socket
network = 'irc.rizon.net'
port = 6667
irc = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
irc.connect((network,port))
irc.send("NICK PyBot\r\n")
irc.send("USER Pybot Pybot Pybot : Python IRC\r\n")
irc.send("JOIN #pychat\r\n")
irc.send("PART #pychat\r\n")
irc.send("QUITE\r\n")
irc.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的是 Python 3,而脚本是为 Python 2 编写的。快速解决方法是通过在字符串文字前添加
b
使字符串文字成为字节文字:在 Python 3 中,
str
是一个字符序列。bytes
是一个字节序列。编辑:我认为 Jean 指的是
socket.send
不能保证发送所有字节这一事实。解决这个问题的快速方法是使用sendall
。You're using Python 3, while the script was written for Python 2. The quick fix is to make the string literals bytes literals by adding a
b
before them:In Python 3, a
str
is a sequence of characters. Abytes
is a sequence of, well, bytes.EDIT: I think Jean is referring to the fact that
socket.send
is not guaranteed to send all the bytes. The quick fix for that is to usesendall
.假设您使用的是 Python 3.x:
您必须通过套接字发送
bytes
,而不是str
。有关
bytes
与str
的详细解释,请参阅字符串深入了解 Python 3 的章节。Assuming you're using Python 3.x:
You have to send
bytes
over a socket, notstr
.For a good explanation of
bytes
vs.str
, see the Strings chapter of Dive Into Python 3.