Python 只能将 str (不是“字节”)连接到 str

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55033372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:32:31  来源:igfitidea点击:

can only concatenate str (not "bytes") to str

pythonpython-3.xstringbyte

提问by Pharah181

import socket
import os

user_url = input("Enter url: ")

host_name = user_url.split("/")[2]
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((host_name, 80))
cmd = 'GET ' + user_url + ' HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
     print(data.decode(),end='\n')

mysock.close()

For some reason im gettin this error

出于某种原因,我开始犯这个错误

Enter url: http://data.pr4e.org/romeo.txt

 7 mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 8 mysock.connect((host_name, 80))
 9 cmd = 'GET ' + user_url + ' HTTP/1.0\r\n\r\n'.encode()
 TypeError: can only concatenate str (not "bytes") to str

输入网址:http: //data.pr4e.org/romeo.txt

 7 mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 8 mysock.connect((host_name, 80))
 9 cmd = 'GET ' + user_url + ' HTTP/1.0\r\n\r\n'.encode()
 TypeError: can only concatenate str (not "bytes") to str

Any ideas what im doing wrong with it?Encoding and decoding seems right to me, and i've trasnfered it using \n before .encode(). This is for a class

任何想法我做错了什么?编码和解码对我来说似乎是正确的,我已经在 .encode() 之前使用 \n 传输它。这是一个班级

回答by chepner

A stris an abstract sequence of Unicode code points; a bytesis a sequence of 8-bit numbers. Python 3 made the distinction between the two very clear and does not allow you to combine them implicitly. A strmay have several valid encodings, and a bytesobject may or may not bethe encoding of a valid Unicode string. (Or, the bytescould be the encoding of multiple differentstrobjects depending on the encoding used to create it.)

Astr是 Unicode 代码点的抽象序列;abytes是一个 8 位数字的序列。Python 3 非常清楚地区分了两者,并且不允许您隐式地将它们组合起来。甲str可能有几个有效的编码,和一个bytes对象可以或可以不一个有效的Unicode字符串的编码。(或者,这bytes可能是多个不同str对象的编码,具体取决于用于创建它的编码。)

'GET 'and user_urlare strobjects, while ' HTTP/1.0\r\n\r\n'.encode()is a bytesobject. You want to encode the entire concatenated string instead.

'GET 'anduser_urlstr对象,而 while' HTTP/1.0\r\n\r\n'.encode()bytes对象。您想对整个连接的字符串进行编码。

cmd = 'GET {} HTTP/1.0\r\n\r\n'.format(user_url).encode()

Or perhaps written to show the steps more clearly,

或者也许是为了更清楚地显示步骤而写的,

cmd = 'GET {} HTTP/1.0\r\n\r\n'.format(user_url)  # still a str
mysock.send(cmd.encode())  # send the encoding of the str

回答by Aran-Fey

The problem is that you're encoding before concatenating:

问题是您在连接之前进行编码:

'GET ' + user_url + ' HTTP/1.0\r\n\r\n'.encode()

You have to concatenate first, and then encode the entire thing:

您必须先连接,然后对整个内容进行编码:

('GET ' + user_url + ' HTTP/1.0\r\n\r\n').encode()