简体   繁体   English

Python错误:参数1必须为缓冲区或字节,不能为str

[英]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.

相关问题 Python 3.1.3 Win 7:csv writerow错误“必须是字节或缓冲区,而不是str” - Python 3.1.3 Win 7: csv writerow Error “must be bytes or buffer, not str” write()参数必须为str,而不是字节数python - write() argument must be str, not bytes python Python - 类型错误:write() 参数必须是 str,而不是字节 - Python - TypeError: write() argument must be str, not bytes join() 参数必须是 str 或 bytes,而不是 'DataFrame' 错误 - join() argument must be str or bytes, not 'DataFrame' error 参数必须是str而不是字节 - Argument must be str not bytes Python sendto错误TypeError:必须为str,而不是字节 - Python sendto Error TypeError: must be str, not bytes TypeError:write()参数必须是str,而不是字节(Python 3 vs Python 2) - TypeError: write() argument must be str, not bytes (Python 3 vs Python 2 ) ElementTree TypeError“write()参数必须是str,而不是Python3中的字节” - ElementTree TypeError “write() argument must be str, not bytes” in Python3 TypeError:必须是str,而不是字节Error - TypeError: must be str, not bytes Error 我该如何解决此python错误? self.stdin.write(input)TypeError:write()参数必须是str,而不是字节 - What do I fix this python error? self.stdin.write(input) TypeError: write() argument must be str, not bytes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM