[英]Python Error: Argument 1 must be buffer or bytes not str
The program is simple, it is meant to connect to an IRC room. 该程序很简单,它旨在连接到IRC房间。 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. 注意:这不是恶意的irc bot或其他内容。 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()
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: 您正在使用Python 3,而该脚本是为Python 2编写的。快速解决方案是在字符串文字前加上字节b
,从而在字符串文字前加上字节b
:
irc.sendall(b"NICK PyBot\r\n")
irc.sendall(b"USER Pybot Pybot Pybot : Python IRC\r\n")
irc.sendall(b"JOIN #pychat\r\n")
irc.sendall(b"PART #pychat\r\n")
irc.sendall(b"QUITE\r\n")
In Python 3, a str
is a sequence of characters. 在Python 3中, str
是字符序列。 A bytes
is a sequence of, well, bytes. 一个bytes
是一个bytes
序列。
EDIT: I think Jean is referring to the fact that socket.send
is not guaranteed to send all the bytes. 编辑:我认为让是指的事实, socket.send
不能保证发送所有字节。 The quick fix for that is to use sendall
. 速战速决因为这是使用sendall
。
Assuming you're using Python 3.x: 假设您使用的是Python 3.x:
You have to send bytes
over a socket, not str
. 您必须通过套接字而不是str
发送bytes
。
irc.send(b"NICK PyBot\r\n")
For a good explanation of bytes
vs. str
, see the Strings chapter of Dive Into Python 3. 有关bytes
vs. str
详细说明,请参阅Dive Into Python 3的Strings一章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.